如何在 javascript 中排序
How to sort in javascript
如果我有这样的数组:
var tab = ['1185 Design','3 D Exhibits','44Doors', '4Concepts','ABC Data','acceleration'];
我想对其进行排序,使小写字母 'a'
元素位于大写字母 'A'
元素之前。
使用Array#sort()
method with String#localeCompare()
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercase based on that return value
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
更新:如果你想按字母顺序排序,小写字母只有在它们相等时才应该有更高的优先级,然后做类似的事情。
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercasebased on that return value in case the letters are equal
if (a.charAt(0).toLowerCase() == b.charAt(0).toLowerCase()) {
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
}
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
您可以使用 sorting with map 大小写反转。
// the array to be sorted
var list = ['1185 Design', '3 D Exhibits', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
// temporary array holds objects with position and sort-value
var mapped = list.map(function (el, i) {
return { index: i, value: el.split('').map(function (a) {
var b = a.toUpperCase();
return a === b ? a.toLowerCase(): b;
}).join('')};
});
// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
// container for the resulting order
var result = mapped.map(function (el) {
return list[el.index];
});
console.log(result);
如果我有这样的数组:
var tab = ['1185 Design','3 D Exhibits','44Doors', '4Concepts','ABC Data','acceleration'];
我想对其进行排序,使小写字母 'a'
元素位于大写字母 'A'
元素之前。
使用Array#sort()
method with String#localeCompare()
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercase based on that return value
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
更新:如果你想按字母顺序排序,小写字母只有在它们相等时才应该有更高的优先级,然后做类似的事情。
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercasebased on that return value in case the letters are equal
if (a.charAt(0).toLowerCase() == b.charAt(0).toLowerCase()) {
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
}
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
您可以使用 sorting with map 大小写反转。
// the array to be sorted
var list = ['1185 Design', '3 D Exhibits', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
// temporary array holds objects with position and sort-value
var mapped = list.map(function (el, i) {
return { index: i, value: el.split('').map(function (a) {
var b = a.toUpperCase();
return a === b ? a.toLowerCase(): b;
}).join('')};
});
// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
// container for the resulting order
var result = mapped.map(function (el) {
return list[el.index];
});
console.log(result);