Sorting in javascript

13 / Mar / 2011 by Sachin 0 comments

Recently in my project I needed to sort an object list on the basis of object name. For the same purpose I created the following function


function sortList(objList) {
 objList.sort(sortByName);
}

function sortByName(a, b) {
 var x = a.name;
 var y = b.name;
 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

This function sorts an object list on the basis of name of object. Hope it helps

Sachin Anand

sachin[at]intelligrape[dot]com

FOUND THIS USEFUL? SHARE IT

Leave a comment -