5

I am not using an associative array. I'm using 1d array like this,

array("1,a","5,b","2,c","8,d","6,f");

How can I sort this array?

The result should be,

array("1,a","2,c","5,b","6,f","8,d");
Rich Adams
  • 26,096
  • 4
  • 39
  • 62
Anish
  • 2,889
  • 1
  • 20
  • 45
  • What problem are you having with [`.sort()`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort)? Is the idea to do a numeric sort by the number first, with secondary sort by the following letter? Or just do a standard string sort on the entire text? That is, how do you want to sort `["1,a", "11,a", "2,b", "2,c"]` – nnnnnn Dec 28 '11 at 06:03
  • sort() is working.. I made a mistake in my code. – Anish Dec 28 '11 at 06:06
  • possible duplicate of [natural sort of text and numbers, JavaScript](http://stackoverflow.com/questions/2802341/natural-sort-of-text-and-numbers-javascript) – outis Dec 28 '11 at 06:14

9 Answers9

4

sort() without a custom sorting function will sort it how you wish (lexicographically).

>>> ["1,a","5,b","2,c","8,d","6,f"].sort();
["1,a", "2,c", "5,b", "6,f", "8,d"]

Note that this will sort the original array. You can make a shallow copy with slice().

If any of your numbers are larger than 9, and you want to sort it via the ordinal of the number, you will need a custom sorting function.

["1,a","5,b","2,c","8,d","6,f"].sort(function(a, b) {
    return parseInt(a, 10) - parseInt(b, 10);
});
alex
  • 479,566
  • 201
  • 878
  • 984
2

Use array.sort() built-in JS function. Documentation here: http://www.w3schools.com/jsref/jsref_sort.asp

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 3
    +1, but I want to advocate MDN (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort) over W3Schools. See why http://w3fools.com/ – nikc.org Dec 28 '11 at 05:58
1

Try this

<script>
var abc=new Array("1,a","5,b","2,c","8,d","6,f");
document.write("<br/>");
document.write(abc.sort());
</script>
kamal
  • 149
  • 6
1

http://jsfiddle.net/Dht33/

var arrayVar = ["1,a","2,c","5,b","6,f","8,d"];

arrayVar.sort()

arrayVar; // "1,a","2,c","5,b","6,f","8,d"
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
1

try with:

function sortNumber(a,b)
{
return (a.split(","))[0] - (b.split(","))[0] ;
}

var n = ["1,a","5,b","2,c","8,d","6,f"];
alert( n.sort(sortNumber) );
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
1

Here's what I do:

var arr1 = new Array("1,a","5,b","2,c","8,d","6,f");
var arr2 = arr1.sort( function(a, b){
    var ax = a.split(",");
    var bx = b.split(",");
    return ax[0]-bx[0];
});

document.getElementById("result").innerHTML = arr2.join(" : ");
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
1

You need to write your own comparison function to compare the numbers, otherwise the default sort() would put something like "40,a" before "5,b".

Here's an example which just splits on the , character and assumes the value before it is numerical. I've added "40,a" into the example to show that it orders numbers >9 correctly.

function mySort(a,b)
{
    return a.split(",", 1) - b.split(",", 1);
}

var theArray = ["1,a", "5,b", "2,c", "40,a", "8,d", "6,f"];
theArray.sort(mySort); // "1,a","2,c","5,b","6,f","8,d","40,a"
Rich Adams
  • 26,096
  • 4
  • 39
  • 62
1

I'm going to go ahead and assume you want to sort by the number part first, sorting numerically, and then have a secondary lexographic (string) sort on the bit after the comma. That way a value like "11,a" will end up after "2,a" - if you just do a default sort "11,a" will end up before "2,a". So:

var a = ["1,a","11,a","8,a","24,z","5,b","2,c","8,d","6,f"];

a.sort(function(a,b) {
   var aparts = a.split(","),
       bparts = b.split(","),
       anum = +aparts[0], // convert first part to number
       bnum = +bparts[0],
       aletter = aparts[1],
       bletter = bparts[1];

   if (anum === bnum)
      return aletter < bletter ? -1 : aletter === bletter ? 0 : -1;
   else
      return anum - bnum;
});

Result:

["1,a", "2,c", "5,b", "6,f", "8,a", "8,d", "11,a", "24,z"]
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Great. I had the similar pblm. I noticed it just now. if the second element is similar then it is not sorting properly. your code helped me. Thanx a lot – Anish Dec 28 '11 at 06:28
  • @Anish , Alex handles this far more elegantly... http://stackoverflow.com/a/8652418/555384 – jondavidjohn Dec 28 '11 at 06:32
  • @jondavidjohn - that may be more elegant, but it is not equivalent because it doesn't include a secondary sort on the alpha part. – nnnnnn Dec 28 '11 at 06:39
0

Try this:

<script type="text/javascript">
var testArray = new Array();
testArray[0] = '1';
testArray[1] = '5';
testArray[2] = '9';
testArray[3] = '8';
testArray[4] = '6';
testArray[5] = '2';
var getlength = testArray.length;
testArray.sort(function(a,b){return a - b});
for(var i=0; i<getlength;i++) {         
    alert(testArray[i]);
}
</script>
Sanjeev Chauhan
  • 3,977
  • 3
  • 24
  • 30