How to Sort an Array of Objects with Jquery or JavaScript

How to sort an array of objects with jquery or javascript

//This will sort your array
function SortByName(a, b){
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

array.sort(SortByName);

Sorting an array of objects by property values

Sort homes by price in ascending order:

homes.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price);
});

Or after ES6 version:

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));

Some documentation can be found here.

For descending order, you may use

homes.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));

How to sort array of objects in jQuery

You can use JavaScript's native sort method and pass your own comparative function, depending on how you wish to sort them: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

e.g. To sort by a property of each object alphabetically

var arr = [{say: "hi"}, {say: "bye"}];

arr.sort(function(a, b) {
if (a.say > b.say) {
return 1;
}
if (a.say < b.say) {
return -1;
}
return 0;
});

// arr now equals [{say: "bye"}, {say: "hi"}];

Working example: http://jsfiddle.net/uKAEz/

Edit: In response to your updates it is clear you are not sorting an array of objects but are sorting an array of strings. It also appears they are in Russian. The following should do what you want:

arr.sort(function(a, b) {
return a.localeCompare(b, "ru");
});

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare for information on localCompare which facilitates sorting unicode strings.

JQUERY Sorting an Array of Objects

Since there are so many silly implementations being proposed that are using jQuery when it is only a waste of resources, I'll propose my own. This is just a straight javascript sort of an array by a property of the object in the array. To do that you just use the array sort method with a custom comparison function that does an alpha comparison of the id value. There is no reason or advantage to involve jQuery in this at all.

activeMembers.sort(function(a, b) {
var aID = a.id;
var bID = b.id;
return (aID == bID) ? 0 : (aID > bID) ? 1 : -1;
});

Note, as requested in the question, this sorts a list of div references in the array. It does not sort the objects in the layout of the page. To do that, you'd have to sort the references list, then rearrange the divs in the page according to the new array order.

If you can rely on the fact that no two IDs are ever the same in your own HTML (since you are never supposed to have two objects with the same ID), you can shorten and speed up the custom sort function to just be this:

activeMembers.sort(function(a, b) {
return (a.id > b.id) ? 1 : -1;
});

JQuery Sorting array of object using date

If lastModifiedDate is numberic value of timestamp then why need to compare date object of those timestamps you can compare those numbers only for efficient execution of sort as below.

completeData.sort(function(a, b){
return a.lastModifiedDate - b.lastModifiedDate;
});

When you need to display date formate you can use Date object for presentation purpose.

Sort array by object key order with jQuery or javascript

Use an array of items and filter that array based on selected array then map filtered array to labels.

Since you are working from ordered array there is no other sorting required

var ops = [    {value:'3',label:'Label 3'},    {value:'1',label:'Label 1'},    {value:'2',label:'Label 2'},];var selected = ['1','3'];
var labels = ops.filter(function(item){ return selected.indexOf(item.value)>-1;}).map(function(item){ return item.label;});
console.log(labels);

How to sort array using Jquery

Please modify your function as below

function SortByName(a, b){ if(a.children){   a.children = a.children.sort(SortByName)  }  if(b.children){   b.children = b.children.sort(SortByName)  }  var aName = a.name.toLowerCase();  var bName = b.name.toLowerCase();  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));}

Sort array of objects by string property value

It's easy enough to write your own comparison function:

function compare( a, b ) {
if ( a.last_nom < b.last_nom ){
return -1;
}
if ( a.last_nom > b.last_nom ){
return 1;
}
return 0;
}

objs.sort( compare );

Or inline (c/o Marco Demaio):

objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))

Or simplified for numeric (c/o Andre Figueiredo):

objs.sort((a,b) => a.last_nom - b.last_nom); // b - a for reverse sort

How can I sort a javascript array of objects numerically and then alphabetically?

I think it's better done just with...

items.sort(function(a, b) { 
return a.id - b.id || a.name.localeCompare(b.name);
});

The second sort will basically negate the first one, so you have to do it once. )

a.id - b.id || a.name.localeCompare(b.name) expression will first compare the ids; only if they are equal, it will compare the names (and return the result of this comparison).

If you need to reverse the ordering, swap the positions (b.id - a.id, etc.) - or just negate the whole thing:

items.sort(function(a, b) { 
return - ( a.id - b.id || a.name.localeCompare(b.name) );
});

Here's the JSFiddle (have to simplify the data a bit to show my point).



Related Topics



Leave a reply



Submit