Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Saturday, September 22, 2012

[JS] Jquery sorting json

source code
$.fn.sort = function(){
    return this.pushStack([].sort.apply(this,arguments),[]);
};​

example (http://jsfiddle.net/wMzXL/)
function sortLastName(a,b){
    if (a.l_name == b.l_name){
        return 0;
    }
    return a.l_name> b.l_name ? 1 : -1;
}
function sortLastNameDesc(a,b){
    return sortLastName(a,b) * -1;
}
var people = [{
    "f_name": "john",
    "l_name": "doe",
    "sequence": "0",
    "title" : "president",
    "url" : "google.com",
    "color" : "333333"
},{
    "f_name": "michael",
    "l_name": "goodyear",
    "sequence": "0",
    "title" : "general manager",
    "url" : "google.com",
    "color" : "333333"
}];
var sorted = $(people).sort(sortLastNameDesc);

source: http://stackoverflow.com/questions/881510/json-sorting-in-jquery