Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a JavaScript array of objects by nested object property?

I have this function to sort a JavaScript array of objects based on a property:

// arr is the array of objects, prop is the property to sort by var sort = function (prop, arr) {     arr.sort(function (a, b) {         if (a[prop] < b[prop]) {             return -1;         } else if (a[prop] > b[prop]) {             return 1;         } else {             return 0;         }     }); }; 

It works with arrays like this:

sort('property', [     {property:'1'},     {property:'3'},     {property:'2'},     {property:'4'}, ]); 

But I want to be able to sort also by nested properties, for example something like:

sort('nestedobj.property', [     {nestedobj:{property:'1'}},     {nestedobj:{property:'3'}},     {nestedobj:{property:'2'}},     {nestedobj:{property:'4'}} ]); 

However this doesn't work because it is not possible to do something like object['nestedobj.property'], it should be object['nestedobj']['property'].

Do you know how could I solve this problem and make my function work with properties of nested objects?

Thanks in advance

like image 924
VerizonW Avatar asked Feb 22 '11 03:02

VerizonW


People also ask

Can you sort an array of objects in JavaScript?

Sort an Array of Objects in JavaScriptTo sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.


1 Answers

You can split the prop on ., and iterate over the Array updating the a and b with the next nested property during each iteration.

Example: http://jsfiddle.net/x8KD6/1/

var sort = function (prop, arr) {     prop = prop.split('.');     var len = prop.length;      arr.sort(function (a, b) {         var i = 0;         while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }         if (a < b) {             return -1;         } else if (a > b) {             return 1;         } else {             return 0;         }     });     return arr; }; 
like image 185
user113716 Avatar answered Sep 20 '22 23:09

user113716