[英]Sorted a javascript array of objects by an object property
Hay, i have an array of objects and i need to sort them (either DESC or ASC) by a certain property of each object. Hay,我有一个对象数组,我需要通过每个对象的某个属性对它们(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? 有人可以帮我弄这个吗?
try this: 试试这个:
data.sort(function(a,b){
return a.date - b.date; //to reverse b.date-a.date
});
We have an application using Angular/TypeScript and found it slightly easier on the eyes using an arrow function. 我们有一个使用Angular / TypeScript的应用程序,使用箭头功能在眼睛上稍微容易一些。 In our case, the following code is example of what we have: 在我们的例子中,以下代码是我们拥有的示例:
data.sort((a, b) => a.SortOrder - b.SortOrder);
Not a huge change but this helps moreso when you have many objects to be sorted. 这不是一个巨大的变化,但是当你有很多要分类的对象时,这会有所帮助。
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);
}));
You can use a custom sort function: 您可以使用自定义排序功能:
function mySort(a,b) {
return (a.date - b.date);
}
data.sort(mySort);
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... 这是我如何按升序使用排序对象数组的示例。“array”是脚本标记中object.copy粘贴的数组,并了解通过控制台工作...
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())
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.