最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sorting an array based on alphabets? - Stack Overflow

programmeradmin2浏览0评论

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"
]

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"
]
Share Improve this question edited Jul 8, 2013 at 9:17 Ionică Bizău 114k94 gold badges310 silver badges487 bronze badges asked Jul 8, 2013 at 9:12 Sumodh NairSumodh Nair 1,6761 gold badge11 silver badges35 bronze badges 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? – Bergi Commented Jul 8, 2013 at 9:14
  • Array seems sorted to me... – mishik Commented Jul 8, 2013 at 9:15
  • 4 What exactly is your desired output? – Christoph Commented Jul 8, 2013 at 9:16
  • @Christoph case insensitive I would imagine – NimChimpsky 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 – Sumodh Nair Commented Jul 8, 2013 at 9:19
 |  Show 2 more ments

3 Answers 3

Reset to default 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)

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);

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.

发布评论

评论列表(0)

  1. 暂无评论