-2

I tried to sort an array using .sort(), but the output is not what I expected.

arr = ["0_11_6_comment", "0_3_6_comment", "0_5_4_comment"]
arr.sort();

Here is my expected output:

["0_3_6_comment", "0_5_4_comment", "0_11_6_comment"]

But I am getting this:

["0_11_6_comment", "0_3_6_comment", "0_5_4_comment"]
user513951
  • 12,445
  • 7
  • 65
  • 82
  • means you want to sort with 2nd interger values : 0_3_6_comment. 3<5<11 or something else? – Gurmeet Feb 19 '14 at 06:04
  • well, you sort it because the 2nd integer value 1<3<5, so it will not change the order by using arr.sort. – blio Feb 19 '14 at 06:07
  • 2
    I don't know what you are actually trying to do since we are still waiting for additional informations, but I guess you're aware that the accepted answer is not able to sort such an array : `["0_1_3_comment", "0_1_1_comment", "0_1_2_comment"]`. Next time, feel free to [reply to comments](http://stackoverflow.com/questions/21872339/sort-in-ascending-order-using-javascript?answertab=votes#comment33115612_21872339) in order to help folks to provide a better help. –  Feb 19 '14 at 06:40

5 Answers5

1

The array is sorting, though it's sorting in lexicographic order, not numerical, and that's probably not what you want. If you want to change how the sort() method sorts, you need to provide your own definition of what "sorting" means. Do this by passing a comparison function as a parameter.

See here for more details:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

blio
  • 473
  • 5
  • 12
Willy
  • 1,055
  • 8
  • 23
0

If you would like to sort based on 2nd integer value. use this:

var arr = ["0_11_6_comment", "0_3_6_comment", "0_5_4_comment"];
alert(arr);
arr = arr.sort(function(a,b) {
    return parseFloat(a.split("_")[1]) - parseFloat(b.split("_")[1]) 
});
alert(arr);

DEMO

Kiran
  • 20,167
  • 11
  • 67
  • 99
0

Try this :

a.sort(function (a, b) {
    // '1_2_3_comment'.split('_')
    // gives ["1", "2", "3", "comment"]
    a = a.split('_');
    b = b.split('_');
    // if a[i] - b[i] = 0 then check next
    return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
});

Read : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort.

0

Here's a JSFiddle with the answer

I suggest reading this.

ary.sort(function (a, b) {
  return getSecondInteger(a) > getSecondInteger(b) ? 1 : -1;
});

function getSecondInteger (str) {
  return Number(str.split('_')[1]);
}
Community
  • 1
  • 1
aleclarson
  • 18,087
  • 14
  • 64
  • 91
0

Try this

var arr = ["0_11_6_comment", "0_3_6_comment", "0_5_4_comment", "1_2_3_comment"];
arr.sort(function(a, b){
  a = a.match(/\d+/g);
  b = b.match(/\d+/g);
  return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
});

console.log(arr)
// result is ["0_3_6_comment", "0_5_4_comment", "0_11_6_comment", "1_2_3"] 

Try demo

Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37
  • Two times slower than [mine](http://stackoverflow.com/a/21872440/1636522) : http://jsperf.com/q-21872339-1636522. Then, your regular expression could be shorter : `/\d+/g` ;) –  Feb 19 '14 at 06:22