6

I need to sort an array based on Alphabets. I have tried sort() method of javascript, but it doesn't work since my array consists of numbers, lowercase letters and uppercase letters. Can anybody please help me out with this? Thanks

For e.g my array is:

[
    "@Basil",
    "@SuperAdmin",
    "@Supreme",
    "@Test10",
    "@Test3",
    "@Test4",
    "@Test5",
    "@Test6",
    "@Test7",
    "@Test8",
    "@Test9",
    "@a",
    "@aadfg",
    "@abc",
    "@abc1",
    "@abc2",
    "@abc5",
    "@abcd",
    "@abin",
    "@akrant",
    "@ankur",
    "@arsdarsd",
    "@asdd",
    "@ashaa",
    "@aviral",
    "@ayush.kansal",
    "@chris",
    "@dgji",
    "@dheeraj",
    "@dsfdsf",
    "@dualworld",
    "@errry",
    "@george",
    "@ggh",
    "@gjhggjghj"
]
7
  • What "Alphabets" would you like it to be sorted after if that doesn't contain letters and numbers? What is wrong with the default Alphabet, what result do you expect? Commented Jul 8, 2013 at 9:14
  • Array seems sorted to me... Commented Jul 8, 2013 at 9:15
  • 4
    What exactly is your desired output? Commented Jul 8, 2013 at 9:16
  • @Christoph case insensitive I would imagine Commented Jul 8, 2013 at 9:19
  • @Bergi: "@" is consistent in the array that I get from the backend,the next letter to it must start with an 'a' or'A'. Say in my example,the first result should be any instances of '@a' then '@aadc' '@abc1' '@abc2' '@chris' and so on Commented Jul 8, 2013 at 9:19

3 Answers 3

7
a.sort(function(a, b) {
    var textA = a.toUpperCase();
    var textB = b.toUpperCase();
    return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});

This should work (jsFiddle)

Sign up to request clarification or add additional context in comments.

Comments

4
function alphabetical(a, b){
     var c = a.toLowerCase();
     var d = b.toLowerCase();
     if (c < d){
        return -1;
     }else if (c > d){
       return  1;
     }else{
       return 0;
     }
}


yourArray.sort(alphabetical);

Comments

1

To sort an array by a key function applied to an element (toUpperCase here), Schwartzian transform, aka "decorate-sort-undecorate" is your technique of choice:

cmp = function(x, y) { return x > y ? 1 : x < y ? -1 : 0 }

sorted = array.map(function(x) {
    return [x.toUpperCase(), x]
}).sort(function(x, y) {
    return cmp(x[0], y[0])
}).map(function(x) {
    return x[1]
})

The major advantage of this approach is that the key function is called exactly once for each element, which can matter when the key is heavy or has side effects.

I realize that you're looking for a simple answer right now, but this might be something for you to consider learning in the future.

4 Comments

No, it yields ["a!", "a"] while it should be ["a", "a!"]. You will need to sort by the decorated part of the array only.
@Bergi: I don't see any ["a!", "a"] in the question.
Of course it's not in the question, OP only posted an example array. How can you be sure that his actual data will contain none of !"#$%&'()*+?
@thg435 well, the argument "It works on the 3 example values" is not much of a valid standpoint for arguing. Although I like the general approach of using a ST, therefore +1 for the general concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.