Sort javascript array of objects by object property

Hay, I have an array of objects, and I need to sort them (DESC or ASC) using the specific property of each object.

Data here

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 an array of data so that the objects are ordered by date.

Can someone help me?

+6
source share
6 answers

Use the Array sort () method

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

try the following:

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

This solution works with any data types:

 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 sorting):

 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); })); 
+3
source

You can use a custom sort function:

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

This is an example of how I use sorting an array of objects in ascending order here. "array" - an array of the object.copy array in the script tag and understanding the work through the 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 ab}); //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()) } 
0
source

I am adding this answer because I see this referenced thread when duplicate labeling and some newer and cleaner solutions are available now.

Another solution is to use a utility library such as Lodash and use its Collection#sortBy . It produces very clean code and promotes a more functional programming style, which leads to fewer errors. In one glance, it becomes clear what the intention is if the code.

The problem with the OP can simply be solved as:

 var sortedObjs = _.sortBy(data, 'date'); 

More information? For instance. we have the following nested object:

 var users = [  { 'user': {'name':'fred', 'age': 48}},  { 'user': {'name':'barney', 'age': 36 }},  { 'user': {'name':'fred'}},  { 'user': {'name':'barney', 'age': 21}} ]; 

Now we can use _. shorthand user.age property to specify the path to the property to be mapped. We will sort user objects by nested age. Yes, this allows you to map nested properties!

 var sortedObjs = _.sortBy(users, ['user.age']); 

Want everything to be the other way around? No problems. Use _. Reverse

 var sortedObjs = _.reverse(_.sortBy(users, ['user.age'])); 

Want to combine both instead of a chain ?

 var sortedObjs = _.chain(users).sortBy('user.age').reverse().value(); 
0
source

Source: https://habr.com/ru/post/889612/


All Articles