我有以下示例数据,我正在try 按多个标准对其进行排序:
-
details.length
(从小到大) -
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);