2 回答

TA贡献1775条经验 获得超8个赞
在以下情况下,您基本上需要考虑函数中的一些不同组合:compare
key === 'users
两者都有一个用户,因此我们比较。
user[0].name
只有一个用户,所以在 .
a
a
b
只有一个用户,所以在 .
b
b
a
没有一个有单个用户,所以我们只是比较。
users.length
它看起来像这样:
if (a.users.length === 1 && b.users.length === 1) {
// If both have a single user, sort by users[0].name:
return a.users[0].name.localeCompare(b.users[0].name);
} else if (a.users.length === 1) {
// If only `a` has a single user, `a` goes before `b`:
return -1;
} else if (b.users.length === 1) {
// If only `b` has a single user, `b` goes before `a`:
return 1;
}
// Otherwise, sort by users.length:
return a.users.length - b.users.length;
在这里,您可以看到它的实际效果:
const array = [{
name: "qw",
users: [
{ name: "Borya" },
],
}, {
name: "qw",
users: [
{ name: "Gorya" },
],
}, {
name: "qw",
users: [
{ name: "Zorya" },
]
}, {
name: "qw",
users: [
{ name: "Var" },
{ name: "Var2" },
],
}, {
name: "qw",
users: [],
}, {
name: "qw",
users: [
{ name: "Aorya" },
],
}, {
name: "qw",
users: [
{ name: "rwerwerwe" },
{ name: "tregdf" },
{ name: "gdfgdf" },
{ name: "Vayetrtertrr2" },
]
}, {
name: "qw",
users: [
{ name: "Dorya" },
],
}];
function orderCustomBy(collection, key, direction) {
const direct = direction === "desc" ? -1 : 1;
let compare;
if (key === "users") {
compare = (a, b) => {
if (a.users.length === 1 && b.users.length === 1) {
// If both have a single user, sort by users[0].name:
return a.users[0].name.localeCompare(b.users[0].name);
} else if (a.users.length === 1) {
// If only `a` has a single user, `a` goes before `b`:
return -1;
} else if (b.users.length === 1) {
// If only `b` has a single user, `b` goes before `a`:
return 1;
}
// Otherwise, sort by users.length:
return a.users.length - b.users.length;
};
} else {
compare = (a, b) => {
if (a === null) return -1;
if (b === null) return 1;
// Just commenting this out because there's no `intlCollator` in here:
// return intlCollator.compare(a, b);
};
}
return [].concat(collection).sort((a, b) => compare(a, b) * direct);
}
console.log(orderCustomBy(array, 'users', 'asc')
.map(item => item.users.length === 1 ? item.users[0].name : item.users.length));
console.log(orderCustomBy(array, 'users', 'desc')
.map(item => item.users.length === 1 ? item.users[0].name : item.users.length));
.as-console-wrapper {
max-height: 100% !important;
}

TA贡献1813条经验 获得超2个赞
由于这些排序标准很复杂,因此最好首先将复杂数组转换为更易于推理和排序的更简单数组。
下面的函数可以做到这一点,生成这个未排序的数组:simplifyArray()
[ "Borya", "Gorya", "Zorya", "2", "0", "Aorya", "4", "Dorya" ]
排序它应该很简单。
function simplifyArray (arr) {
return arr.map(el => {
let length = el.users.length;
if (length === 1) { return el.users[0].name; }
else return String(length);
});
}
const array = [
{
name: "qw",
users: [
{ name: "Borya" }
]
},
{
name: "qw",
users: [
{ name: "Gorya" }
]
},
{
name: "qw",
users: [
{ name: "Zorya" }
]
},
{
name: "qw",
users: [
{ name: "Var" },
{ name: "Var2" }
]
},
{
name: "qw",
users: []
},
{
name: "qw",
users: [
{ name: "Aorya" }
]
},
{
name: "qw",
users: [
{ name: "rwerwerwe" },
{ name: "tregdf" },
{ name: "gdfgdf" },
{ name: "Vayetrtertrr2" }
]
},
{
name: "qw",
users: [
{ name: "Dorya" }
]
},
];
const simplerArray = simplifyArray(array);
console.log(simplerArray);
添加回答
举报