我想用字符串对数组排序.但是,大写字母应该总是在相应的小写字母之前(例如顺序:A a B b...).
为了存档,我try 使用localCompare
与caseFirst: "upper"
和sensitivity: "case"
:
const input = ["ADR 2023", "Added Values", "DEMO build", "DEMO Build", "2nd Outlook", "demo award"]
const output = input.sort((a, b) => a.localeCompare(b, "en-US", { caseFirst: "upper", sensitivity: "case" }))
console.log(output)
我希望输出是:
["2nd Outlook", "ADR 2023", "Added Values", "DEMO Build", "DEMO build", "demo award"]
不幸的是,输出是:
["2nd Outlook", "Added Values", "ADR 2023", "demo award", "DEMO Build", "DEMO build"]
如何获得预期订单?在这个例子中,我使用了en-US
作为locale.然而,在我的实际项目中,它可以是任何支持的语言环境,长度为五个字符.