仅使用排序函数,在另一个数组的基础上按词法顺序对数组元素进行排序
Sort Element of array on the basis of another array in lexical order by using just sort function
假设我们有两个数组参数和顺序,我想先放置数组元素的顺序,然后在结果中对所有其他元素进行排序。
我不想在排序时使用顺序数组。
并且还想使用排序功能。
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort']
let order = ['Door', 'Container Number', 'Cases', 'Items/sort']
Output ->['Door', 'Container Number', 'Cases', 'Items/sort','fjjfh','hello','hi','jdjfr']
您需要检查每个被排序的项目是否在 order
数组中,并优先考虑其中的一个(如果它们都在,则优先考虑它们的相对位置),请参阅评论:
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort'];
let order = ['Door', 'Container Number', 'Cases', 'Items/sort'];
// Wanted: ["Door", "Container Number", "Cases", "Items/sort","fjjfh","hello","hi","jdjfr"]
parameter.sort((a, b) => {
const apos = order.indexOf(a);
const bpos = order.indexOf(b);
if (apos !== -1) {
// `a` is in the `order` array
if (bpos === -1) {
// `b` isn't, put `a` first
return -1;
}
// Both are, put them in the order they have in `order`
return apos - bpos;
} else if (bpos !== -1) {
// `b` is in the `order` array, `a` isn't; `b` comes first
return 1;
} else {
// Neither is in the `order` array, put them in lexicographical order
return a.localeCompare(b);
}
});
console.log(parameter);
如果您以这种方式对真正庞大的数组进行排序and/or如果order
是真正庞大的,您可能希望在开始之前在 order
中创建一个 map
位置,这样您就不必为每个位置查找线性遍历 order
:
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort'];
let order = ['Door', 'Container Number', 'Cases', 'Items/sort'];
// Wanted: ["Door", "Container Number", "Cases", "Items/sort","fjjfh","hello","hi","jdjfr"]
const orderMap = new Map(order.map((value, index) => [value, index]));
parameter.sort((a, b) => {
const apos = orderMap.get(a);
const bpos = orderMap.get(b);
if (apos !== undefined) {
// `a` is in the `order` array
if (bpos === undefined) {
// `b` isn't, put `a` first
return -1;
}
// Both are, put them in the order they have in `order`
return apos - bpos;
} else if (bpos !== undefined) {
// `b` is in the `order` array, `a` isn't; `b` comes first
return 1;
} else {
// Neither is in the `order` array, put them in lexicographical order
return a.localeCompare(b);
}
});
console.log(parameter);
但是你不需要为小型或中型阵列操心。
假设我们有两个数组参数和顺序,我想先放置数组元素的顺序,然后在结果中对所有其他元素进行排序。
我不想在排序时使用顺序数组。 并且还想使用排序功能。
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort']
let order = ['Door', 'Container Number', 'Cases', 'Items/sort']
Output ->['Door', 'Container Number', 'Cases', 'Items/sort','fjjfh','hello','hi','jdjfr']
您需要检查每个被排序的项目是否在 order
数组中,并优先考虑其中的一个(如果它们都在,则优先考虑它们的相对位置),请参阅评论:
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort'];
let order = ['Door', 'Container Number', 'Cases', 'Items/sort'];
// Wanted: ["Door", "Container Number", "Cases", "Items/sort","fjjfh","hello","hi","jdjfr"]
parameter.sort((a, b) => {
const apos = order.indexOf(a);
const bpos = order.indexOf(b);
if (apos !== -1) {
// `a` is in the `order` array
if (bpos === -1) {
// `b` isn't, put `a` first
return -1;
}
// Both are, put them in the order they have in `order`
return apos - bpos;
} else if (bpos !== -1) {
// `b` is in the `order` array, `a` isn't; `b` comes first
return 1;
} else {
// Neither is in the `order` array, put them in lexicographical order
return a.localeCompare(b);
}
});
console.log(parameter);
如果您以这种方式对真正庞大的数组进行排序and/or如果order
是真正庞大的,您可能希望在开始之前在 order
中创建一个 map
位置,这样您就不必为每个位置查找线性遍历 order
:
let parameter = ['Door', 'fjjfh','Container Number','jdjfr', 'Cases', 'hello', "hi", 'Items/sort'];
let order = ['Door', 'Container Number', 'Cases', 'Items/sort'];
// Wanted: ["Door", "Container Number", "Cases", "Items/sort","fjjfh","hello","hi","jdjfr"]
const orderMap = new Map(order.map((value, index) => [value, index]));
parameter.sort((a, b) => {
const apos = orderMap.get(a);
const bpos = orderMap.get(b);
if (apos !== undefined) {
// `a` is in the `order` array
if (bpos === undefined) {
// `b` isn't, put `a` first
return -1;
}
// Both are, put them in the order they have in `order`
return apos - bpos;
} else if (bpos !== undefined) {
// `b` is in the `order` array, `a` isn't; `b` comes first
return 1;
} else {
// Neither is in the `order` array, put them in lexicographical order
return a.localeCompare(b);
}
});
console.log(parameter);
但是你不需要为小型或中型阵列操心。