{title}


Code RSS Feed

Code

Sort an Array of Objects by an Object Property in JQuery

TOPICS:
  • JQuery

This is a quick snippet that often comes in handy - you have a bunch of objects which are being added to an array in no particular order, and you want to sort that array into some logical order, using a common property that all of the objects possess. The code is simple:

var array = [(idnamevalue),(idnamevalue),(idnamevalue)];

function 
SortByName(ab){
  
var aName a.name.toLowerCase();
  var 
bName b.name.toLowerCase(); 
  return ((
aName bName) ? -: ((aName bName) ? 0));
}

array.sort(SortByName);