按值排序JSON

Kem*_*min 59 jquery json

我有一个非常简单的JSON对象,如下所示:

{
   "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"
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

现在,这是从我的服务器端代码返回的,我运行jQuery.each以形成必要的html并输出结果.

现在我正在做的是向包含我的排序信息的服务器发送一个AJAX调用...例如"Title DESC"并重新运行SQL查询以返回新的结果集.但我想避免这种情况,并使用jQuery对生成的JSON进行排序,以防止往返服务器和多个数据库访问.

我怎样才能使用jQuery实现这一目标?

Sea*_*ean 119

试试这个优雅和高效.

jQuery很好,但它不适合在这里排序,除非你没有原始数组方便.只需编写一个函数,将属性名称作为字符串,将顺序(升序或降序)作为布尔值,编写一个简单的比较函数,并使用本机js sort()函数.这样您就不必为每个属性编写单独的排序函数:

var people = [
    {
        "f_name": "john",
        "l_name": "doe",
        "sequence": "0",
        "title" : "president",
        "url" : "google.com",
        "color" : "333333",
    }
    // etc
];

function sortResults(prop, asc) {
    people.sort(function(a, b) {
        if (asc) {
            return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
        } else {
            return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
        }
    });
    renderResults();
}
Run Code Online (Sandbox Code Playgroud)

然后:

sortResults('l_name', true);
Run Code Online (Sandbox Code Playgroud)

在这里玩一个工作的例子.

  • 当我再输入几行数据时,这似乎对我不起作用。每次尝试时似乎排序都不一样。这是小提琴:http://jsfiddle.net/Jsar8/ 任何帮助表示赞赏。 (2认同)
  • @Hugolpz准确地说,Chrome,Opera <10,Dolphin和FF <3实现了不稳定的排序算法(根据http://stackoverflow.com/questions/3026281和我可以在互联网上找到的最佳信息).FF 3 +,Opera 10 +,IE和Safari中使用的算法是稳定的.我之前评论中的相关SO问题对Array.sort()实现有更深入的了解. (2认同)

Hug*_*lpz 22

演示:http://jsfiddle.net/VAKrE/1019/

成功传递相等的值(保持相同的顺序).灵活:处理上升(123)或后代(321),适用于数字,字母和unicodes.适用于所有经过测试的设备(Chrome,Android默认浏览器,FF).

鉴于以下数据:

var people = [ 
{ 'myKey': 'A', 'status': 0 },
{ 'myKey': 'B', 'status': 3 },
{ 'myKey': 'C', 'status': 3 },
{ 'myKey': 'D', 'status': 2 },
{ 'myKey': 'E', 'status': 7 },
...
];
Run Code Online (Sandbox Code Playgroud)

按升序或逆序排序:

function sortJSON(data, key, way) {
    return data.sort(function(a, b) {
        var x = a[key]; var y = b[key];
        if (way === '123' ) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }
        if (way === '321') { return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }
    });
}

people2 = sortJSON(people,'status', '321'); // 123 or 321
alert("2. After processing (0 to x if 123; x to 0 if 321): "+JSON.stringify(people2));
Run Code Online (Sandbox Code Playgroud)


Tah*_*tar 12

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);  
Run Code Online (Sandbox Code Playgroud)


tcq*_*cql 7

如果你不介意使用外部库,Lodash有很多很棒的工具

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 = _.sortBy(people, "l_name")
Run Code Online (Sandbox Code Playgroud)

您还可以按多个属性进行排序.这是一个显示它在行动的插件