Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort JavaScript array of Objects based on one of the object's properties [duplicate]

Tags:

I've got an array of objects, each of which has a property name, a string. I'd like to sort the array by this property. I'd like them sorted in the following way..

`ABC` `abc` `BAC` `bac` etc... 

How would I achieve this in JavaScript?

like image 211
Skizit Avatar asked Mar 24 '11 15:03

Skizit


People also ask

How to sort an array of objects in a JavaScript function?

We are required to write a JavaScript function that takes in one such array of objects. The function should sort this array based on two different properties − sort by the higher "resFlow" value, but with the lowest "resHP" value.

How to sort array elements according to the return value?

Thankfully, the function takes an optional compareFunction parameter, which causes the array elements to be sorted according to the return value of the compare function. Let’s say that foo and bar are the two elements being compared by the compare function, and the return value of the compare function is set up as follows:

How to compare an array of objects based on properties?

However, the traditional sort () function may lag behind at times to compare an array of objects based on some property. So we can create a user-defined compare function to be used with the sort () function. This method compares the properties of the items in an array. The example below shows how to write your own comparison function.

How do you sort an array using callback functions?

The way .sort works is defined by what you return from the callback function you pass in. If you return: <= -1 then a will come before b. Thus, by calculating the difference between the two ages, this "naturally" gives the correct values to properly sort your array.


1 Answers

There are 2 basic ways:

var arr = [{name:"ABC"},{name:"BAC"},{name:"abc"},{name:"bac"}];  arr.sort(function(a,b){   var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();   return alc > blc ? 1 : alc < blc ? -1 : 0;  }); 

or

arr.sort(function(a,b){   return a.name.toLowerCase().localeCompare(b.name.toLowerCase());  }); 

Be aware that the 2nd version ignore diacritics, so a and à will be sorted as the same letter.

Now the problem with both these ways is that they will not sort uppercase ABC before lowercase abc, since it will treat them as the same.

To fix that, you will have to do it like this:

arr.sort(function(a,b){   var alc = a.name.toLowerCase(), blc = b.name.toLowerCase();   return alc > blc ? 1 : alc < blc ? -1 : a.name > b.name ? 1 : a.name < b.name ? -1 : 0; }); 

Again here you could choose to use localeCompare instead if you don't want diacritics to affect the sorting like this:

arr.sort(function(a,b){   var lccomp = a.name.toLowerCase().localeCompare(b.name.toLowerCase());   return lccomp ? lccomp : a.name > b.name ? 1 : a.name < b.name ? -1 : 0; }); 

You can read more about sort here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort

like image 91
Martin Jespersen Avatar answered Sep 18 '22 20:09

Martin Jespersen