Сортировка
https://stackoverflow.com/questions/57493054/sorting-based-on-alphabets-and-then-by-numbers
https://www.educative.io/answers/how-to-check-if-a-string-is-a-number-javascript
Изначально в компоненте Explorer.tsx написано вот так:
sortFn: (a, b) => {
// Sort order: folders first, then files. Sort folders and files alphabetically
if ((!a.file && !b.file) || (a.file && b.file)) {
// numeric: true: Whether numeric collation should be used, such that "1" < "2" < "10"
// sensitivity: "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A
return a.displayName.localeCompare(b.displayName, undefined, {
numeric: true,
sensitivity: "base",
})
}
if (a.file && !b.file) {
return 1
} else {
return -1
}
}
Нужно добавить проверку на то, является ли одно из сравниваемых чисел числом и в зависимости от этого выводить в прямом или обратном порядке:
sortFn: (a, b) => {
// Sort order: folders first, then files. Sort folders and files alphabetically
if ((!a.file && !b.file) || (a.file && b.file)) {
// Проверяем, что одно из сравниваемых начинается с числа и тогда выводим в обратном порядке.
var isNumberRegEx = /^[0-9]*$/;
if (isNumberRegEx.test(a.displayName[0])) {
// numeric: true: Whether numeric collation should be used, such that "1" < "2" < "10"
// sensitivity: "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A
return b.displayName.localeCompare(a.displayName, undefined, {
numeric: true,
sensitivity: "base",
})
}
else {
// numeric: true: Whether numeric collation should be used, such that "1" < "2" < "10"
// sensitivity: "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A
return a.displayName.localeCompare(b.displayName, undefined, {
numeric: true,
sensitivity: "base",
})
}
}
if (a.file && !b.file) {
return 1
} else {
return -1
}
}