Try this. Even your object is not having the property based on which you are trying to sort also will get handled.
Just call it by sending property with object.
var sortObjectByProperty = function(property,object){
console.time("Sorting");
var sortedList = [];
emptyProperty = [];
tempObject = [];
nullProperty = [];
$.each(object,function(index,entry){
if(entry.hasOwnProperty(property)){
var propertyValue = entry[property];
if(propertyValue!="" && propertyValue!=null){
sortedList.push({key:propertyValue.toLowerCase().trim(),value:entry});
}else{
emptyProperty.push(entry);
}
}else{
nullProperty.push(entry);
}
});
sortedList.sort(function(a,b){
return a.key < b.key ? -1 : 1;
//return a.key < b.key?-1:1; // Asc
//return a.key < b.key?1:-1; // Desc
});
$.each(sortedList,function(key,entry){
tempObject[tempObject.length] = entry.value;
});
if(emptyProperty.length>0){
tempObject.concat(emptyProperty);
}
if(nullProperty.length>0){
tempObject.concat(nullProperty);
}
console.timeEnd("Sorting");
return tempObject;
}