Sorting JSON by values

前端 未结 6 1094
北荒
北荒 2020-11-22 12:16

I have a very simple JSON object like the following:

{
   \"people\":[
      {
         \"f_name\":\"john\",
         \"l_name\":\"doe\",
         \"sequence         


        
6条回答
  •  不要未来只要你来
    2020-11-22 12:42

    jQuery.fn.sort = function() {  
        return this.pushStack( [].sort.apply( this, arguments ), []);  
    };  
    
     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",
    }]
    
    sorted=$(people).sort(sortLastNameDesc);  
    

提交回复
热议问题