3 回答
TA贡献1859条经验 获得超6个赞
您可以排序String#localeCompare
并选择按点值列排序。
最后取前三项Array#slice
。
const
array = [{ orgId: 101, dId: 494, name: "Test1", sequesnce: "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" }, { orgId: 102, dId: 442, name: "Test2", sequesnce: "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR" }, { orgId: 103, sequesnce: "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR", dId: 494, name: "Test3" }, { orgId: 103, sequesnce: "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR", dId: 494, name: "Test3" }],
top3 = array
.sort((a, b) => b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base' }))
.slice(0, 3)
console.log(top3);
.as-console-wrapper { max-height: 100% !important; top: 0; }
TA贡献1856条经验 获得超17个赞
如果序列较短且前导值较高,则需要在排序前扩展元组 - 这不适用于 localeCompare
const maxLen = 4;
const expandTuple = tpl => {
tpl = tpl.split('.');
while (tpl.length < maxLen) tpl.unshift("000");
return tpl.map(x => x.padStart(3, "0")).join('.');
};
const array1 = [{ "orgId": 101, "dId": 494, "name": "Test1",
"sequence": "6.408.06.0 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR" },{"orgId": 102,"dId": 442,"name": "Test2",
"sequence": "9.80.709.5 Sequence Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",},{"orgId": 103,
"sequence": "99.1.1 NOTE THIS IS SHORTER Sequence Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,
"sequence": "8.208.409.0 Sequence Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR","dId": 494,"name": "Test3",},{"orgId": 103,
"sequence": "9.408.90.3 Sequence Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR","dId": 494, "name": "Test3", }
]
const three = array1.sort((a,b) => {
const aTub = a.sequence.split(" ")[0];
const bTub = b.sequence.split(" ")[0];
if (expandTuple(aTub)<expandTuple(bTub)) return 1
if (expandTuple(aTub)>expandTuple(bTub)) return -1
return 0;
}).slice(0,3)
console.log(three)
// ---- compare other solution which fails on the shorter sequence
console.log(array1
.sort((a, b) => b.sequence.localeCompare(a.sequence, undefined, { numeric: true, sensitivity: 'base' }))
.slice(0, 3)
)
TA贡献1829条经验 获得超6个赞
首先,使用Array.map,您可以仅生成项目对象数组sequesne。
并使用Array.sort,您可以按降序对数组进行排序。
并使用Array.splice,您可以从排序数组中提取前 3 项。
const array1 = [{
"orgId": 101,
"dId": 494,
"name": "Test1",
"sequesnce": "6.408.06.0 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR"
},
{
"orgId": 102,
"dId": 442,
"name": "Test2",
"sequesnce": "9.80.709.5 Sequesnce Date : Mon 06/04/2019 (Sections: P4.00344) Italy CR",
},
{
"orgId": 103,
"sequesnce": "9.138.309.0 Sequesnce Date : Mon 06/03/2019 (Sections: P4.45600) Spain HR",
"dId": 494,
"name": "Test3",
},
{
"orgId": 103,
"sequesnce": "8.208.409.0 Sequesnce Date : Mon 10/03/2019 (Sections: P4.568787) Spain HR",
"dId": 494,
"name": "Test3",
},
{
"orgId": 103,
"sequesnce": "9.408.90.3 Sequesnce Date : Mon 08/06/2019 (Sections: P4.00344) Italy CR",
"dId": 494,
"name": "Test3",
}
];
const sortedArr = array1.map(({ sequesnce }) => ({ sequesnce })).sort((a, b) => (b.sequesnce.localeCompare(a.sequesnce, undefined, { numeric: true, sensitivity: 'base'})));
const topCount = 3;
console.log(sortedArr.splice(0, topCount));
添加回答
举报