4

I have to sort an array of strings like this:

var arr = ["akaw","waka","kawa","akwa"];

The type of sort must be by a specific letter, W in this case so my function must return this array:

arr = ["waka","kawa","akwa","akaw"];

It's a dynamic array and I don't know the number of words in array and if each words have no letter W, one W or some W.

Do you know a type of sort function to do that?

Thx!

1
  • just sort by W in this example Commented Jun 15, 2013 at 8:52

1 Answer 1

4

Just compare the index of the letter.

arr.sort(function(a,b) {
    return a.indexOf("w") - b.indexOf("w");
})

DEMO: http://jsfiddle.net/KrqmM

waka
kawa
akwa
akaw
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.