2 回答

TA貢獻1775條經(jīng)驗 獲得超8個贊
在以下情況下,您基本上需要考慮函數(shù)中的一些不同組合: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條經(jīng)驗 獲得超2個贊
由于這些排序標(biāo)準(zhǔn)很復(fù)雜,因此最好首先將復(fù)雜數(shù)組轉(zhuǎn)換為更易于推理和排序的更簡單數(shù)組。
下面的函數(shù)可以做到這一點,生成這個未排序的數(shù)組:simplifyArray()
[ "Borya", "Gorya", "Zorya", "2", "0", "Aorya", "4", "Dorya" ]
排序它應(yīng)該很簡單。
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);
添加回答
舉報