按对象属性对 JavaScript 对象数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6212737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:53:26  来源:igfitidea点击:

Sorted a javascript array of objects by an object property

javascriptarrayssortingobject

提问by dotty

Hay, i have an array of objects and i need to sort them (either DESC or ASC) by a certain property of each object.

嘿,我有一个对象数组,我需要按每个对象的某个属性对它们进行排序(DESC 或 ASC)。

Here's the data

这是数据

obj1 = new Object;
obj1.date = 1307010000;

obj2 = new Object;
obj2.date = 1306923600;

obj3 = new Object;
obj3.date = 1298974800;

obj4 = new Object;
obj4.date = 1306923600;

obj5 = new Object;
obj5.date = 1307096400;

data = [obj1,obj2,obj3,obj4,obj5];

Now, i want to order the data array so that the objects are in order by date.

现在,我想对数据数组进行排序,以便对象按日期排序。

Can someone help me with this?

有人可以帮我弄这个吗?

回答by Phil

Use the Array sort()method

使用数组sort()方法

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

回答by jerjer

try this:

试试这个:

data.sort(function(a,b){
   return a.date - b.date; //to reverse b.date-a.date
});

回答by yaka

This solution works with any type of data:

此解决方案适用于任何类型的数据:

sort_array_by = function(field, reverse, pr){
  reverse = (reverse) ? -1 : 1;
  return function(a,b){
    a = a[field];
    b = b[field];
    if (typeof(pr) != 'undefined'){
      a = pr(a);
      b = pr(b);
    }
    if (a<b) return reverse * -1;
    if (a>b) return reverse * 1;
    return 0;
  }
}

Then, use it like this (reverse sort):

然后,像这样使用它(反向排序):

data.sort(sort_array_by('date', true, function(a){
   return new Date(a);
}));

As another example, you can sort it by a property of type "integer":

作为另一个例子,您可以按“整数”类型的属性对其进行排序:

data.sort(sort_array_by('my_int_property', true, function(a){
   return parseInt(a);
}));

回答by My Stack Overfloweth

We have an application using Angular/TypeScript and found it slightly easier on the eyes using an arrow function. In our case, the following code is example of what we have:

我们有一个使用 Angular/TypeScript 的应用程序,并发现使用箭头函数在眼睛上更容易一些。在我们的例子中,以下代码是我们所拥有的示例:

data.sort((a, b) => a.SortOrder - b.SortOrder);

Not a huge change but this helps moreso when you have many objects to be sorted.

不是一个巨大的变化,但是当您有许多对象要排序时,这会更有帮助。

回答by devprog

You can use a custom sort function:

您可以使用自定义排序功能:

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

回答by ABDULMOIZ

This is an example of how i use sorting array of objects in ascending order here "array" is array of an object.copy paste in a script tag and understand working through console...

这是我如何使用升序排序对象数组的示例,此处“数组”是对象数组。复制粘贴到脚本标签中,并了解通过控制台工作...

function OBJECT(){
    this.PROPERTY1 =Math.floor(Math.random()*10+1) ;


}

OBJECT.prototype.getPROPERTY1=function (){
    return(this.PROPERTY1);
}
OBJECT.prototype.setPROPERTY1=function (PROPERTY){
    this.PROPERTY1=PROPERTY;
}

var array= new Array();
console.log("unsorted object")
for(var a=0;a<10;a++)
{
array.push(new OBJECT());
console.log(array[a].getPROPERTY1())
}


function sorting() {
    for(var i in array){
        array[i].setPROPERTY1((array[i].getPROPERTY1()*1))
             //that is just for sorting an integer value escape this line if not an                                      
            //integer property
    }

    var arr=new(Array);
    var temp1=new(Array);
    for(var i in array){
        temp1.push(array[i]);
    }
     var temporary=new(Array)
    for(var i in array)
    {
        var temp = array[i].getPROPERTY1();
        arr.push(temp);
    }
    arr.sort(function(a,b){return a-b});
    //the argument above is very important

    console.log(arr)
    for(var i in arr){

        for(var j in temp1)
            if(arr[i]==temp1[j].getPROPERTY1())
                break;

        temporary.push(temp1[j])


        temp1.splice(j,1)//just this statement works for me

    }
    array.length=0;
    for(var i in temporary)
    {
        array.push(temporary[i])
    }



}


     sorting();
      console.log("sorted object")
 for(var a=0;a<10;a++)
 {
             console.log(array[a].getPROPERTY1())
 }