如何用jquery或javascript(复制)对对象数组进行排序

时间:2022-08-15 15:59:02

This question already has an answer here:

这个问题已经有了答案:

I have an array of objects:

我有一个对象数组:

var array = [(id, name, value),(id, name, value)]; //and so on

How do I get the array to be sorted in ascending order of the atribute name (array[i][1])?

如何让数组按照atribute名称(array[I][1])的升序排序?

I've tried to do this: array[i][1].sort(), but that doesn't work.

我试过这么做:array[I][1].sort(),但这行不通。

Please help me!

请帮助我!

Edit: the array can contain more than two objects! It can contain hundreds.

编辑:数组可以包含两个以上的对象!它可以包含数百人。

Edit: Why is this question marked as a duplicate, when it was asked 2 years before the "duplicated" question?

编辑:为什么这个问题被标记为重复,在“重复”的问题出现前2年被问到?

6 个解决方案

#1


371  

//This will sort your array
function SortByName(a, b){
  var aName = a.name.toLowerCase();
  var bName = b.name.toLowerCase(); 
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

array.sort(SortByName);

#2


33  

var array = [[1, "grape", 42], [2, "fruit", 9]];

array.sort(function(a, b)
{
    // a and b will here be two objects from the array
    // thus a[1] and b[1] will equal the names

    // if they are equal, return 0 (no sorting)
    if (a[1] == b[1]) { return 0; }
    if (a[1] > b[1])
    {
        // if a should come after b, return 1
        return 1;
    }
    else
    {
        // if b should come after a, return -1
        return -1;
    }
});

The sort function takes an additional argument, a function that takes two arguments. This function should return -1, 0 or 1 depending on which of the two arguments should come first in the sorting. More info.

sort函数接受一个附加参数,一个接受两个参数的函数。这个函数应该返回-1、0或1,这取决于排序中两个参数中的哪一个。更多信息。

I also fixed a syntax error in your multidimensional array.

我还修复了多维数组中的语法错误。

#3


26  

//objects
var array = [{id:'12', name:'Smith', value:1},{id:'13', name:'Jones', value:2}];
array.sort(function(a, b){
    var a1= a.name.toLower(), b1= b.name.toLower();
    if(a1== b1) return 0;
    return a1> b1? 1: -1;
});

//arrays
var array =[ ['12', ,'Smith',1],['13', 'Jones',2]];
array.sort(function(a, b){
    var a1= a[1], b1= b[1];
    if(a1== b1) return 0;
    return a1> b1? 1: -1;
});

#4


13  

data.sort(function(a,b) 
{
   return a.val - b.val;
});

#5


11  

the sort method contains an optional argument to pass a custom compare function.

sort方法包含一个可选参数来传递自定义比较函数。

Assuming you wanted an array of arrays:

假设你想要数组:

var arr = [[3, "Mike", 20],[5, "Alex", 15]];

function compareName(a, b)
{

  if (a[1] < b[1]) return -1;
  if (a[1] > b[1]) return 1;
  return 0;
}
arr.sort(compareName);

Otherwise if you wanted an array of objects, you could do:

否则,如果你想要一个对象数组,你可以这样做:

function compareName(a, b)
{

  if (a.name < b.name) return -1;
  if (a.name > b.name) return 1;
  return 0;
}

#6


3  

Well, it appears that instead of creating a true multidimensional array, you've created an array of (almost) JavaScript Objects. Try defining your arrays like this ->

好吧,似乎不是创建一个真正的多维数组,而是创建了一个(几乎)JavaScript对象数组。试着像这样定义你的数组->。

var array = [ [id,name,value], [id,name,value] ]

Hopefully that helps!

希望这可以帮助!

#1


371  

//This will sort your array
function SortByName(a, b){
  var aName = a.name.toLowerCase();
  var bName = b.name.toLowerCase(); 
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

array.sort(SortByName);

#2


33  

var array = [[1, "grape", 42], [2, "fruit", 9]];

array.sort(function(a, b)
{
    // a and b will here be two objects from the array
    // thus a[1] and b[1] will equal the names

    // if they are equal, return 0 (no sorting)
    if (a[1] == b[1]) { return 0; }
    if (a[1] > b[1])
    {
        // if a should come after b, return 1
        return 1;
    }
    else
    {
        // if b should come after a, return -1
        return -1;
    }
});

The sort function takes an additional argument, a function that takes two arguments. This function should return -1, 0 or 1 depending on which of the two arguments should come first in the sorting. More info.

sort函数接受一个附加参数,一个接受两个参数的函数。这个函数应该返回-1、0或1,这取决于排序中两个参数中的哪一个。更多信息。

I also fixed a syntax error in your multidimensional array.

我还修复了多维数组中的语法错误。

#3


26  

//objects
var array = [{id:'12', name:'Smith', value:1},{id:'13', name:'Jones', value:2}];
array.sort(function(a, b){
    var a1= a.name.toLower(), b1= b.name.toLower();
    if(a1== b1) return 0;
    return a1> b1? 1: -1;
});

//arrays
var array =[ ['12', ,'Smith',1],['13', 'Jones',2]];
array.sort(function(a, b){
    var a1= a[1], b1= b[1];
    if(a1== b1) return 0;
    return a1> b1? 1: -1;
});

#4


13  

data.sort(function(a,b) 
{
   return a.val - b.val;
});

#5


11  

the sort method contains an optional argument to pass a custom compare function.

sort方法包含一个可选参数来传递自定义比较函数。

Assuming you wanted an array of arrays:

假设你想要数组:

var arr = [[3, "Mike", 20],[5, "Alex", 15]];

function compareName(a, b)
{

  if (a[1] < b[1]) return -1;
  if (a[1] > b[1]) return 1;
  return 0;
}
arr.sort(compareName);

Otherwise if you wanted an array of objects, you could do:

否则,如果你想要一个对象数组,你可以这样做:

function compareName(a, b)
{

  if (a.name < b.name) return -1;
  if (a.name > b.name) return 1;
  return 0;
}

#6


3  

Well, it appears that instead of creating a true multidimensional array, you've created an array of (almost) JavaScript Objects. Try defining your arrays like this ->

好吧,似乎不是创建一个真正的多维数组,而是创建了一个(几乎)JavaScript对象数组。试着像这样定义你的数组->。

var array = [ [id,name,value], [id,name,value] ]

Hopefully that helps!

希望这可以帮助!