如何在javascript中排序对象数组

时间:2016-03-09 08:21:55

标签: javascript arrays sorting object

我想要排序数组,下面显示的是bayid

Array是数组名称

[
 Object { bayid="35",  status=0},
 Object { bayid="1",  status=0},
 Object { bayid="37",  status=0}
]

Array.sort(function(a,b){return b.bayid >a.bayid})

我不确定这个函数返回的是什么,但我想编写一个返回排序数组的函数,如下所示

[
 Object { bayid="37",  status=0},
 Object { bayid="35",  status=0},
 Object { bayid="1",  status=0}
]

怎么做请帮忙

4 个答案:

答案 0 :(得分:2)

    var objArray = [{ bayid:"35",  status:0},{ bayid:"1",  status:0}, { bayid:"37",  status:0}];

    function compare(a,b) {
    
      if (a.bayid < b.bayid )
        return 1;
      else if (a.bayid > b.bayid)
        return -1;
      else 
        return 0;
    }
    
    console.log(objArray.sort(compare));

答案 1 :(得分:1)

原始代码和某些解决方案的问题在于它们错误sort字符串值:b.bayid > a.bayid

这似乎正常工作,直到我们将最后一个元素设置为bayid =“100”并发现它返回"35" > "100" = true。数组排序不正确。

要修复此错误,我们可以使用parseInt(a.bayid)或只是在前面添加一个加号(+ a.bayid)来按数字而不是字符串值排序。现在所有人都对阵地感到高兴。

运行下面的代码段以查看两种排序方法的结果。

var _a = [
  
    {bayid: "35",  status: 0 },
    {bayid: "1",   status: 0 },
    {bayid: "100", status: 0 }

].sort(function(a, b) { 
  return b.bayid > a.bayid;   // <== to compare string values
});

print('Test 1: Sort by string', _a );


_a = [
  
    {bayid: "35",  status: 0 },
    {bayid: "1",   status: 0 },
    {bayid: "100", status: 0 }

].sort(function(a, b) { 
  return +b.bayid > +a.bayid;   // <== to compare numeric values
});

print('Test 2: Sort by number', _a );


function print( s, o ) {
  window.stdout.innerHTML += s + '\n' + JSON.stringify(o, false, '  ') + '\n\n';
}
Scroll down to view result:<br>
<xmp id="stdout"></xmp>

答案 2 :(得分:0)

您的代码可以正常工作。 阅读更多关于.sort函数here

的信息

&#13;
&#13;
    var arr = [{ bayid:"35",  status:0}, { bayid:"1",  status:0}, { bayid:"37",  status:0}];

    var sortedArr = arr.sort(function(a,b) {return b.bayid > a.bayid});

    console.log(sortedArr);
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

这些JSON项的语法似乎在您的代码中有点混乱。 试试这个

terminate called after throwing an instance of 'bsoncxx::v_noabi::exception'
  what():  unset document::element
[zy1989:20594] *** Process received signal ***
[zy1989:20594] Signal: Aborted (6)
[zy1989:20594] Signal code:  (-6)
[zy1989:20594] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x10d10)[0x7f05d3b65d10]
[zy1989:20594] [ 1] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x37)[0x7f05d37c0267]
[zy1989:20594] [ 2] /lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7f05d37c1eca]
[zy1989:20594] [ 3] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x16d)[0x7f05d471106d]
[zy1989:20594] [ 4] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5eee6)[0x7f05d470eee6]
[zy1989:20594] [ 5] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5ef31)[0x7f05d470ef31]
[zy1989:20594] [ 6] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5f149)[0x7f05d470f149]
[zy1989:20594] [ 7] /usr/local/lib/libbsoncxx.so._noabi(_ZNK7bsoncxx7v_noabi8document7element4typeEv+0xdd)[0x7f05d49cfe0d]
[zy1989:20594] [ 8] /usr/local/lib/libbsoncxx.so._noabi(_ZN7bsoncxx7v_noabi7to_jsonENS0_8document7elementE+0x286)[0x7f05d49d8926]
[zy1989:20594] [ 9] ./ArticleIndexComputation(_Z7groupbyN8mongocxx7v_noabi6cursor8iteratorElRSt6vectorIN7bsoncxx7v_noabi8document4viewESaIS7_EERSt3mapIiS3_IiSaIiEESt4lessIiESaISt4pairIKiSD_EEE+0x186)[0x415826]
[zy1989:20594] [10] ./ArticleIndexComputation(main+0x1181)[0x417161]
[zy1989:20594] [11] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f05d37aba40]
[zy1989:20594] [12] ./ArticleIndexComputation(_start+0x29)[0x415439]
[zy1989:20594] *** End of error message ***