【问题标题】:Sorting JSON in JavaScript在 JavaScript 中对 JSON 进行排序
【发布时间】:2021-12-14 02:32:14
【问题描述】:

我知道之前有人问过类似的问题,我在这里提到了这个问题:sort json object in javascript 但我仍然找不到我的问题的答案。所以我来了。我有一个 JSON 对象结构如下:

 [
  {
    "toothNumber": "01",
    "name": "John"
  },
  {
    "toothNumber": "18",
    "name": "John"
  },
  {
    "toothNumber": "19",
    "name": "John"
  },
  {
    "toothNumber": "17",
    "name": "John"
  },
  {
    "toothNumber": "01,32",
    "name": "John"
  },
  {
    "toothNumber": "25,32",
    "name": "John"
  },
  {
    "toothNumber": "",
    "name": "John"
  },
  {
    "toothNumber": "15",
    "name": "John"
  }
]

当我使用下面的代码进行排序时,我没有得到预期的结果:

json.sort(function(a, b){
    return a.toothNumber - b.toothNumber;
});

下面是实际结果,不是我预期的结果。任何帮助将不胜感激。

实际结果:

[
  {
    "toothNumber": "",
    "name": "John"
  },
  {
    "toothNumber": "01",
    "name": "John"
  },
  {
    "toothNumber": "15",
    "name": "John"
  },
  {
    "toothNumber": "17",
    "name": "John"
  },
  {
    "toothNumber": "18",
    "name": "John"
  },
  {
    "toothNumber": "19",
    "name": "John"
  },
  {
    "toothNumber": "01,32",
    "name": "John"
  },
  {
    "toothNumber": "25,32",
    "name": "John"
  }
]

预期结果:

[
    {
        "toothNumber": "",
        "name": "John"
    },
    {
        "toothNumber": "01",
        "name": "John"
    },
    {
        "toothNumber": "01,32",
        "name": "John"
    },
    {
        "toothNumber": "15",
        "name": "John"
    },
    {
        "toothNumber": "17",
        "name": "John"
    },
    {
        "toothNumber": "18",
        "name": "John"
    },
    {
        "toothNumber": "19",
        "name": "John"
    },
    {
        "toothNumber": "25,32",
        "name": "John"
    }
]

【问题讨论】:

  • 你减去的是字符串而不是数字。先转换成数字。
  • 不确定您希望它如何工作。牙号首先是一个字符串,在某些项目中它是一个多值字符串。比如"25,32" - "18"的结果应该是什么?
  • @jarmod 这些是保存在客户端数据库中的有效值。如果它们都是一个索赔的一部分,他们会保存多个以逗号分隔的牙齿编号。我同意吗,不,但这是我拥有的数据
  • 表示完全没有问题。我只是强调一个事实,即导致 "25,32" - "18" 的实现没有什么意义。

标签: javascript json sorting javascript-objects


【解决方案1】:

按字符串排序,而不是数字比较。

const json = [  {    "toothNumber": "01",    "name": "John"  },  {    "toothNumber": "18",    "name": "John"  },  {    "toothNumber": "19",    "name": "John"  },  {    "toothNumber": "17",    "name": "John"  },  {    "toothNumber": "01,32",    "name": "John"  },  {    "toothNumber": "25,32",    "name": "John"  },  {    "toothNumber": "",    "name": "John"  },  {    "toothNumber": "15",    "name": "John"  }];

json.sort(function(a, b){
    return a.toothNumber > b.toothNumber ? 1 : (a.toothNumber === b.toothNumber ? 0 : -1 );
});

console.log(json);

【讨论】:

    【解决方案2】:

    你可以这样做

    const json = [  {    "toothNumber": "01",    "name": "John"  },  {    "toothNumber": "18",    "name": "John"  },  {    "toothNumber": "19",    "name": "John"  },  {    "toothNumber": "17",    "name": "John"  },  {    "toothNumber": "01,32",    "name": "John"  },  {    "toothNumber": "25,32",    "name": "John"  },  {    "toothNumber": "",    "name": "John"  },  {    "toothNumber": "15",    "name": "John"  }];
    
    json.sort(function(a, b) {
       return a.toothNumber.split(",")[0] - b.toothNumber.split(",")[0];
    });
    
    console.log(json)

    【讨论】:

    • parseInt 是多余的。
    • @Spectric 是的,当我将其更改为与 sn-p 一起使用时,它被留下了。但无论如何, split 返回字符串数组,所以parseInt 是可取的
    猜你喜欢
    • 2011-05-12
    • 2013-07-15
    • 2014-05-22
    • 2023-03-03
    • 2011-10-29
    • 2021-06-21
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多