我有以下示例数据,我正在try 按多个标准对其进行排序:

  1. details.length(从小到大)
  2. details.type(按字母顺序:索赔人,FellowPassenger)

如果我按details.length排序,它似乎可以工作,但details.type在多个或单一排序标准版本上都不起作用,因为数据不是按字母顺序排序的(Claimant应该排在Fellow的前面).

因此,输出应为:

 sortedByMultiple:  [
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
    ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      },
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
    ],
    "state": "accepted"
  }
]

const groupedStackOverflow = [
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
  ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [
    {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "rejected"
  }
]

console.log("groupedStackOverflow: ",groupedStackOverflow )


const sortedByMultiple = groupedStackOverflow.sort(function (a, b) {
  return a.details.length - b.details.length || a.details.type - b.details.type ;
});

console.log("sortedByMultiple: ", sortedByMultiple);


const sortedByOne = groupedStackOverflow.sort(function (a, b) {
  return a.details.type - b.details.type ;
});

console.log("sortedByOne: ", sortedByOne);

推荐答案

try 使用字符串比较更改排序条件,因为a.details.type - b.details.type返回NaN.

试试这个:

const groupedStackOverflow = [
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  },
  {
    "document_file_name": "4020600_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      },
      {
        "id": 20656,
        "type": "Dummy",
        "name": "First Name Last Name"
      }
    ],
    "state": "accepted"
  },
  {
    "document_file_name": "4020890_FileName.pdf",
    "details": [
      {
        "id": 10000,
        "type": "Fellow",
        "name": "Fellow First Name Last Name"
      }
  ],
    "state": "rejected"
  },
  {
    "document_file_name": "4020672_FileName.pdf",
    "details": [
    {
        "id": 20656,
        "type": "Claimant",
        "name": "First Name Last Name"
      }
    ],
    "state": "rejected"
  }
]

console.log("groupedStackOverflow: ",groupedStackOverflow )

const stringComparison = function (a,b) {
  return a.type.localeCompare(b.type);
}
const sortedByMultiple = groupedStackOverflow.sort(function (a, b) {
      if(a.details.length > 1)  a.details.sort(stringComparison);
      if(b.details.length > 1)  b.details.sort(stringComparison);
      return a.details.length - b.details.length || stringComparison(a.details[0],b.details[0]);
});

console.log("sortedByMultiple: ", sortedByMultiple);

你也在以错误的方式访问type,details是一个数组,这就是为什么它会给你一个错误.现在,您可以修改该代码段以根据需要进行调整

Javascript相关问答推荐

在拖放时阻止文件打开

Chart.js 4.4.2,当悬停在一个数据点上时,如何在工具提示中拥有多个数据点/标签?

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

使用Promise.All并发解决时,每个promise 的线性时间增加?

编辑文本无响应.onClick(扩展脚本)

提交链接到AJAX数据结果的表单

类构造函数不能在没有用With Router包装的情况下调用

连接到游戏的玩家不会在浏览器在线游戏中呈现

基于props 类型的不同props ,根据来自接口的值扩展类型

Rxjs流中生成IMMER不能在对象上操作

在表单集中保存更改时删除';禁用';

当我点击一个按钮后按回车键时,如何阻止它再次被点击

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

:host::ng-Deep不将样式应用于material 复选框

已在EventListener中更新/更改异步的React.js状态对象,但不会导致组件重新呈现

鼠标进入,每秒将图像大小减小5%

TypeScrip Types-向流程对象添加属性

如何创建在<;span>;中附加每个字符的自定义元素?

Redux不更新状态

为什么它是Object.hasOwn(x,y)!==Reflect.ownKeys(X).如果x是CSSStyleClaimation对象,而y是像';z-index';这样的连字符属性,则包含(Y)?