如何在javascript中排序对象数组

时间:2021-08-14 15:59:40

I want sort array which is shown below by bayid

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

Array is the array Name

Array是数组Name

[
 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})

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

I am not sure what is this function returning but i want to write a function which returns the sorted array as shown below

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

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

how to do it please help

怎么做请帮忙

4 个解决方案

#1


1  

The problem with the original code and with some of the solutions is that they incorrectly sort by string value: b.bayid > a.bayid

原始代码和一些解决方案的问题是它们错误地按字符串值排序:b.bayid> a.bayid

This appears to work correctly until we set the last element to bayid="100" and find that it returns "35" > "100" = true. The array is sorted incorrectly.

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

To fix this bug, we could use parseInt(a.bayid) or simply prefix it with a plus like (+a.bayid) to sort by numeric rather than string values. And now all is happy in array land.

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

Run the snippet below to see the results of both methods of sorting.

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

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


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));

#3


0  

your code works as it should. read up more on .sort function here

你的代码应该正常工作。在这里阅读更多.sort函数

    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);

#4


-1  

The syntax of those JSON items seems to be a bit confused in your code. Try this instead

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

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

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

#1


1  

The problem with the original code and with some of the solutions is that they incorrectly sort by string value: b.bayid > a.bayid

原始代码和一些解决方案的问题是它们错误地按字符串值排序:b.bayid> a.bayid

This appears to work correctly until we set the last element to bayid="100" and find that it returns "35" > "100" = true. The array is sorted incorrectly.

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

To fix this bug, we could use parseInt(a.bayid) or simply prefix it with a plus like (+a.bayid) to sort by numeric rather than string values. And now all is happy in array land.

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

Run the snippet below to see the results of both methods of sorting.

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

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


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));

#3


0  

your code works as it should. read up more on .sort function here

你的代码应该正常工作。在这里阅读更多.sort函数

    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);

#4


-1  

The syntax of those JSON items seems to be a bit confused in your code. Try this instead

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

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

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