当数组包含字母时按数字顺序对数组进行排序
Sorting an Array by number order when it contains letters
我有一个包含组信息的数组。我按组名对它们进行排序,如下所示:
groups.sort().sort(function (a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
这 returns 他们的名字顺序:
10, 11, 13, 7, 8, 9, A10, A11, A7, A8, A9
但是我想这样订购它们:
7, A7, 8, A8, 9, A9, 10, A10, 11, A11, 13
谁能帮我解决这个问题?
首先将项目排序为字符串,接下来使用排序 lambda 中将数组项目转换为 Number
再次排序。类似于:
const x = `Z9,10,11,B11,13,7,8,B8,9,A10,A11,A7,A8,A9`.split(`,`)
.map(v => ({name: v}));
// first sort on string values
const sortedx = x.sort( (a, b) => a.name.localeCompare(b.name))
// second sort on numeric values
.sort( (a, b) => +a.name.replace(/\D/g, ``) - +b.name.replace(/\D/g, ``));
console.log(`${sortedx.map(v => v.name )}`);
您可以获得数字并按此数字排序,如果找不到值则为零
const
getNumber = s => s.toString().match(/\d+/)?.[0] || 0,
array = ['A1', 1, 10, 11, 13, 7, 8, 9, 'A10', 'A11', 'A7', 'A8', 'A9', '1'];
array.sort((a, b) => getNumber(a) - getNumber(b) || a.toString().localeCompare(b));
console.log(...array);
通过为 属性 访问器使用对象和函数的方法。
const
getNumber = s => s.toString().match(/\d+/)?.[0] || 0,
customSort = (a, b) => getNumber(a) - getNumber(b) || a.toString().localeCompare(b),
getValue = ({ value }) => value,
sortBy = (sortFn, valueFn) => (a, b) => sortFn(valueFn(a), valueFn(b)),
array = [{ value: 'A1' }, { value: 1 }, { value: 10 }, { value: 11 }, { value: 13 }, { value: 7 }, { value: 8 }, { value: 9 }, { value: 'A10' }, { value: 'A11' }, { value: 'A7' }, { value: 'A8' }, { value: 'A9' }, { value: '1A' }];
array.sort(sortBy(customSort, getValue));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你可以使用这个:
groups.sort().sort(function (a, b) {
var aa = parseInt(a.name.replace(/[^\d.-]/g, ''));
var bb = parseInt(b.name.replace(/[^\d.-]/g, ''));
if (aa < bb)
return -1;
if (aa > bb)
return 1;
// When == 0, order using full name, not only the number
if (a.name < b.name)
return -1;
return a.name > b.name ? 1 : 0;
});
正则表达式只取数字部分,解析为int用于比较。对于相同的数字(例如“7”和“A7”),我们使用原始名称来排序。
我有一个包含组信息的数组。我按组名对它们进行排序,如下所示:
groups.sort().sort(function (a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
这 returns 他们的名字顺序:
10, 11, 13, 7, 8, 9, A10, A11, A7, A8, A9
但是我想这样订购它们:
7, A7, 8, A8, 9, A9, 10, A10, 11, A11, 13
谁能帮我解决这个问题?
首先将项目排序为字符串,接下来使用排序 lambda 中将数组项目转换为 Number
再次排序。类似于:
const x = `Z9,10,11,B11,13,7,8,B8,9,A10,A11,A7,A8,A9`.split(`,`)
.map(v => ({name: v}));
// first sort on string values
const sortedx = x.sort( (a, b) => a.name.localeCompare(b.name))
// second sort on numeric values
.sort( (a, b) => +a.name.replace(/\D/g, ``) - +b.name.replace(/\D/g, ``));
console.log(`${sortedx.map(v => v.name )}`);
您可以获得数字并按此数字排序,如果找不到值则为零
const
getNumber = s => s.toString().match(/\d+/)?.[0] || 0,
array = ['A1', 1, 10, 11, 13, 7, 8, 9, 'A10', 'A11', 'A7', 'A8', 'A9', '1'];
array.sort((a, b) => getNumber(a) - getNumber(b) || a.toString().localeCompare(b));
console.log(...array);
通过为 属性 访问器使用对象和函数的方法。
const
getNumber = s => s.toString().match(/\d+/)?.[0] || 0,
customSort = (a, b) => getNumber(a) - getNumber(b) || a.toString().localeCompare(b),
getValue = ({ value }) => value,
sortBy = (sortFn, valueFn) => (a, b) => sortFn(valueFn(a), valueFn(b)),
array = [{ value: 'A1' }, { value: 1 }, { value: 10 }, { value: 11 }, { value: 13 }, { value: 7 }, { value: 8 }, { value: 9 }, { value: 'A10' }, { value: 'A11' }, { value: 'A7' }, { value: 'A8' }, { value: 'A9' }, { value: '1A' }];
array.sort(sortBy(customSort, getValue));
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
你可以使用这个:
groups.sort().sort(function (a, b) {
var aa = parseInt(a.name.replace(/[^\d.-]/g, ''));
var bb = parseInt(b.name.replace(/[^\d.-]/g, ''));
if (aa < bb)
return -1;
if (aa > bb)
return 1;
// When == 0, order using full name, not only the number
if (a.name < b.name)
return -1;
return a.name > b.name ? 1 : 0;
});
正则表达式只取数字部分,解析为int用于比较。对于相同的数字(例如“7”和“A7”),我们使用原始名称来排序。