var a=[{a:1,b:"YES"},{a:2,b:"YES"},{a:5,b:"NOT"},{a:0,b:"NOT"},{a:0,b:"YES"}]
function sortc(x,y){
if (x.b=="NOT" || y.b=="NOT")
return Infinity ;
return (Number(x.a)-Number(y.a))
}
console.log(a.sort(sortc));
结果是:
0: {a: 1, b: "YES"}
1: {a: 2, b: "YES"}
2: {a: 5, b: "NOT"}
3: {a: 0, b: "NOT"}
4: {a: 0, b: "YES"}
预期结果是(带有b =“ yes”的排序组件。):
{ "a": 0, "b": "YES" }
{ "a": 1, "b": "YES" }
{ "a": 5, "b": "NOT" }
{ "a": 0, "b": "NOT" }
{ "a": 2, "b": "YES" }
您不仅可以使用
Array#sort()
提取所有要分类的所有项目。 索 在原始数组上go,只替换应分类的任何东西,将其余的物品放在其位置。
var a = [
{ a: 1, b: "YES" },
{ a: 2, b: "YES" },
{ a: 5, b: "NOT" },
{ a: 0, b: "NOT" },
{ a: 0, b: "YES" }
]
//get only `b: "YES"` items
const dataToSort = a.filter(item => item.b === "YES");
//sort them
dataToSort.sort((x, y) => x.a - y.a);
//replace only items that need to be sorted
const it = dataToSort.values()
for (let i = 0; i < a.length; i++) {
if (a[i].b === "NOT")
continue;
a[i] = it.next().value;
}
console.log(a);
const it = dataToSort.values()
for (const [key, item] of a.entries()) { //use the key-value iterator from the array
if (item.b === "NOT")
continue;
[a[key]] = it; //array destructuring internally advances an iterator
}
var a = [
{ a: 1, b: "YES" },
{ a: 2, b: "YES" },
{ a: 5, b: "NOT" },
{ a: 0, b: "NOT" },
{ a: 0, b: "YES" }
]
//get only `b: "YES"` items
const dataToSort = a.filter(item => item.b === "YES");
//sort them
dataToSort.sort((x, y) => x.a - y.a);
//replace only items that need to be sorted
const it = dataToSort.values()
for (const [key, item] of a.entries()) {
if (item.b === "NOT")
continue;
[a[key]] = it;
}
console.log(a);
最终,可以通过辅助器功能
和少数小实用程序函数更方便地使其更方便
/* library code */
const transformArg = transform => f => (...args) => f(transform(...args));
function* filter(predicate, it) {
for (const item of it) {
if (predicate(item))
yield item;
}
}
/* /library code */
var a = [
{ a: 1, b: "YES" },
{ a: 2, b: "YES" },
{ a: 5, b: "NOT" },
{ a: 0, b: "NOT" },
{ a: 0, b: "YES" }
]
/* helpers */
//extract the `b` key in this case so we don't need to repeat it.
const getSortableAttribute = transformArg(({b}) => b);
//get the value from key-value pair
const getValue = transformArg(([, value]) => value);
//check if the attribute is "YES"
const isSortable = getSortableAttribute(attr => attr === "YES");
const dataToSort = a.filter(isSortable);
dataToSort.sort((x, y) => x.a - y.a);
const it = dataToSort.values()
//iterate only over sortable key-value pairs by re-using the `isSortable` filter
for (const [key, item] of filter(getValue(isSortable), a.entries())) {
[a[key]] = it;
}
console.log(a);
sort
的方法,但使用
Proxy
length
和索引来构成访问。
.as-console-wrapper { max-height: 100% !important; top: 0; }
输入参数只是确定可排序元素
的谓词const
sortOnly = (array, isSortable) => {
const nonSortableIndicies = new Set();
let shift = 0;
var proxyArray = new Proxy(array, {
get(target, prop) {
if (!isFinite(prop)) return target[prop];
if (isSortable(target[prop])) {
return target[prop];
} else
nonSortableIndicies.add(+prop);
return undefined;
},
set(target, prop, value) {
if (!isFinite(prop)) {
target[prop] = value;
return true;
}
let index = +prop + shift;
if (index >= target.length) return true;
while (nonSortableIndicies.has(index)) {
index++;
}
shift = index - prop;
target[index] = value;
return true;
}
});
return proxyArray;
},
array = [{
a: 1,
b: "YES"
}, {
a: 2,
b: "YES"
}, {
a: 5,
b: "NOT"
}, {
a: 0,
b: "NOT"
}, {
a: 0,
b: "YES"
}];
sortOnly(array, (val) => val.b !== 'NOT')
.sort((a, b) => a.a - b.a)
console.log(array);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}