如何在JavaScript中排序数组

时间:2022-05-09 15:01:10

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

9 个解决方案

#1


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:

我将继续假设你想先按数字部分排序,按数字排序,然后在逗号后面的位上有一个辅助字典(字符串)排序。这样,像“11,a”这样的值会在“2,a”之后结束——如果你只做一个默认的排序“11,a”会在“2,a”之前结束。所以:

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

#2


4  

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

sort()不使用自定义排序函数就可以按自己的意愿进行排序(在字典上)。

>>> ["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().

注意,这将对原始数组进行排序。您可以用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.

如果您的任何一个数字大于9,并且您想通过数字的序号对其进行排序,您将需要一个自定义排序函数。

["1,a","5,b","2,c","8,d","6,f"].sort(function(a, b) {
    return parseInt(a, 10) - parseInt(b, 10);
});

#3


2  

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

使用array.sort()内置的JS函数。文档:http://www.w3schools.com/jsref/jsref_sort.asp

#4


1  

http://jsfiddle.net/Dht33/

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"

#5


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(" : ");

#6


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

您需要编写自己的比较函数来比较这些数字,否则默认的sort()将会在“5、b”之前放置“40,a”之类的东西。

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.

这里有一个例子,它只对字符进行分割,并在数值前假设值。我在示例中添加了“40,a”,以显示它对>9的订单是正确的。

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"

#7


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

#8


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>

#9


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>

#1


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:

我将继续假设你想先按数字部分排序,按数字排序,然后在逗号后面的位上有一个辅助字典(字符串)排序。这样,像“11,a”这样的值会在“2,a”之后结束——如果你只做一个默认的排序“11,a”会在“2,a”之前结束。所以:

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

#2


4  

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

sort()不使用自定义排序函数就可以按自己的意愿进行排序(在字典上)。

>>> ["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().

注意,这将对原始数组进行排序。您可以用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.

如果您的任何一个数字大于9,并且您想通过数字的序号对其进行排序,您将需要一个自定义排序函数。

["1,a","5,b","2,c","8,d","6,f"].sort(function(a, b) {
    return parseInt(a, 10) - parseInt(b, 10);
});

#3


2  

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

使用array.sort()内置的JS函数。文档:http://www.w3schools.com/jsref/jsref_sort.asp

#4


1  

http://jsfiddle.net/Dht33/

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"

#5


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(" : ");

#6


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

您需要编写自己的比较函数来比较这些数字,否则默认的sort()将会在“5、b”之前放置“40,a”之类的东西。

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.

这里有一个例子,它只对字符进行分割,并在数值前假设值。我在示例中添加了“40,a”,以显示它对>9的订单是正确的。

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"

#7


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

#8


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>

#9


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>