12/18/2014

Sorting array of strings & objects

Ref: http://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript

[array].sort(function(a, b){
 var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase()
 if (nameA < nameB) //sort string ascending
  return -1 
 if (nameA > nameB)
  return 1
 return 0 //default return value (no sorting)
});

No comments: