javascript中区分大小写的排序,其中所有大写字母都在所有小写字母之前



我需要以大写字母优先的方式对两个字符串进行排序,即使是不同的字母,例如:

蚂蚁之前

CDCD之前

我可以使用localCompare进行这样的排序吗?

const sortString = (a, b) => String(a).localeCompare(b);

不,您不能(也不需要(使用localeCompare。你所描述的只是标准的字符串比较,它将字母按字母顺序排列,大写字母放在小写字母之前!

所以你所需要做的就是

arr.sort((a, b) => +(a>b)||-(b>a))

字符串的哪个与默认的回退比较功能相同,所以您可以只执行

arr.sort()

My函数在第一个字符串大于时返回1,在第一个小于时返回-1,在第二个字符串等于时返回0。

// return value like compare function
const findCompareValue = (flag) => {
if (flag) {
return -1;
} else {
return 1;
}
}
const compareCaseSensitive = (a, b) => {
// find min length just in case two string don't have the same length
const minLen = a.length > b.length ? b.length : a.length;
let i = 0;
const minLowerCaseCharCode = 97;
const maxLowerCaseCharCode = 122;
const minUpperCaseCharCode = 65;
const maxUpperCaseCharCode = 90;
while (true && i !== minLen) {
// get char code
const aCharCode = a[i].charCodeAt();
const bCharCode = b[i].charCodeAt();
// check whether character is uppercase
const isACharUpperCase = minUpperCaseCharCode <= aCharCode && aCharCode <= maxUpperCaseCharCode;
const isBCharUpperCase = minUpperCaseCharCode <= bCharCode && bCharCode <= maxUpperCaseCharCode;
if (isACharUpperCase) {
if (isBCharUpperCase  && bCharCode !== aCharCode) {
// two characters are uppercase and different
return findCompareValue(aCharCode < bCharCode);
} else if (aCharCode !== bCharCode) {
// second character is lowercase
return 1;
}
} else {
if (isBCharUpperCase) {
// second string is uppercase
return -1;
} else if (aCharCode !== bCharCode) {
// two characters are lowercase and different
return findCompareValue(aCharCode < bCharCode);
}
}
i += 1;
}
return 0;
}
const a = 'CD';
const b = 'cD';
console.log('result', compareCaseSensitive(a, b));

你能尝试一下吗,不确定是否满足所有要求。。。

输入:[Cat',March',aJan',Feb',Feb,Dd,Dd,ant,Cat,AMC

输出:

const months = ['Cat', 'March', 'aJan','Feb', 'Feb', 'Dd', 'DD', 'ant', 'cat', 'AMC', 'AMBulance', 'Feb','Cd' , 'CD']
function _sort(a,b) {

const aLenth = a.length
const bLenth = b.length

const aArr = a.match(/^[A-Z]*/g )
const bArr = b.match(/^[A-Z]*/g )
const isCapA = Array.isArray(aArr) && !!aArr[0].length
const isCapB = Array.isArray(bArr) && !!bArr[0].length

const isBothCapital = isCapA && isCapB // then check usual way
const isBothSimple = !isCapA && !isCapB // then check usual way

//console.log(a, b, isBothCapital, aArr.length, bArr.length)
if (isBothCapital || isBothSimple){
if (a === b )  {
return 0 
}
if (aArr[0].length ===  bArr[0].length) {
return a.localeCompare(b);
}
//return a.localeCompare(b);
return  aArr[0].length - bArr[0].length ? 1 : -1
}
if (isCapA) {
return -1
}

return 1

}
const _simpSort = (a,b ) => {
return a.localeCompare(b)
}
const capLetters = months.filter(x => x.match(/^[A-Z]/g))
const simpLetters = months.filter(x => x.match(/^[a-z]/g))
const formatted = [...capLetters.sort(_simpSort), ...simpLetters.sort(_simpSort)]
const groupBy = formatted.reduce((acc, i) => {

const isExists = acc[i.charAt(0)]
const ele = typeof isExists !== "undefined" ? isExists : []

const tmp = { [i.charAt(0)]: [i, ...ele] }
return { ...acc, ...tmp}
}, {})
const final = []
for (ele in groupBy) {
const tmpRow = groupBy[ele].sort(_sort)
final.push(tmpRow)
}
console.log(final.join().split(","))

您很幸运,因为A-Z字符代码比A-Z小,所以我们可以使用charCode进行排序。

棘手的一点是循环遍历每个字符。

const list = ['Cat', 'Cat2', 'ant', 'CD', 'Cd', 'cD', 'Ab', 'zb', 'zC'];
const sortByCharcode = (a, b) => {
if (a.charCodeAt(0) < b.charCodeAt(0)) return -1;
if (a.charCodeAt(0) > b.charCodeAt(0)) return 1;
return 0;
}
const sortFunc = (strA, strB) => {
// Loop through each character.
for (let i = 0; i < strB.length; i++) {
const a = strA[i];
const b = strB[i];
const compareResult = sortByCharcode(a, b); // Compare each index
// If result is -1 or 1, return it, if it's still 0 then move to next character.
if (compareResult !== 0) return compareResult;
}
// If strB.length run out, A comes before B
return 1;
}
const sortedList = list.sort(sortFunc);
console.log(sortedList);

最新更新