gpt4 book ai didi

javascript - 按字母顺序对数组进行排序,然后按大写字母排序,然后按小写字母排序

转载 作者:行者123 更新时间:2023-12-02 02:09:23 25 4
gpt4 key购买 nike

我想按字母顺序对数组进行排序,然后按大写字母排序,然后按小写字母排序。

const i = ["aA", "BA", "Aa", "aa", "Ba", "AA"];

//I tired so far.
i.sort((a, b) => a.localeCompare(b));

console.log(i);

//expected output
const o = ["AA", "Aa", "aA", "aa", "BA", "Ba"];

最佳答案

您可以使用caseFirst指定大写字母优先于小写字母的选项:

const i = ["aA", "BA", "Aa", "aa", "Ba", "AA"];

i.sort((a, b) => a.localeCompare(b, 'en-US', {caseFirst: 'upper'}));

console.log(i);

关于javascript - 按字母顺序对数组进行排序,然后按大写字母排序,然后按小写字母排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67917865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com