var array = [
{
"checked": true,
"name":"Name",
"type": {
"id": 1,
"tag": "tag"
}
},
{
"checked": true,
"name":"Name",
"type": {
"id": 3,
"tag": "tag"
}
},
{
"checked": false,
"name":"Name",
"type": {
"id": 2,
"tag": "tag"
}
},
];
我想通过选中和type.id对数组进行排序。我正在使用下面的排序代码,但是“ type.id”似乎有问题,因为如果选中的话,我的列表就不会按那些列表进行分组。
sortByPriority(array, ['checked', 'type.id']);
sortByPriority(data, priorities) {
if (priorities.length == 0 || data.length == 0) {
return data;
}
const nextPriority = priorities[0];
const remainingPriorities = priorities.slice(1);
const matched = data.filter(item => item.hasOwnProperty(nextPriority));
const remainingData = data.filter(item => !item.hasOwnProperty(nextPriority));
return this.sortByPriority(matched, remainingPriorities)
.sort((a, b) => (a[nextPriority] > b[nextPriority]) ? 1 : -1)
.concat(this.sortByPriority(remainingData, remainingPriorities));
}
关于如何对类型对象进行排序的任何想法?
(我无法使用能够根据数组中的对象进行排序的通用排序器找到另一个带有答案的问题)
答案 0 :(得分:2)
您可以对嵌套属性使用嵌套方法,并采用一系列函数比较值。
function sortByPriority(array, keys, fn) {
function getValue(o, k) {
return k.split('.').reduce((p, l) => (p || {})[l], o);
}
return array.sort((a, b) => {
var d;
keys.some((k, i) => d = fn[i](getValue(a, k), getValue(b, k)));
return d;
});
}
var array = [{ checked: false, name: "Name", type: { id: 2, tag: "tag" } }, { checked: true, name: "Name", type: { id: 3, tag: "tag" } }, { checked: true, name: "Name", type: { id: 1, tag: "tag" } }];
sortByPriority(array, ['checked', 'type.id'], [(a, b) => b - a, (a, b) => a - b]);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
在数组上重载sort
方法,然后您可以根据您的排序条件切换<
和>
。请注意,如果第一个排序上的return 0
相等,则将它们删除,以便在checked
值相等的情况下移至第二个排序。
var array = [
{
"checked": true,
"name":"Name",
"type": {
"id": 1,
"tag": "tag"
}
},
{
"checked": true,
"name":"Name",
"type": {
"id": 3,
"tag": "tag"
}
},
{
"checked": false,
"name":"Name",
"type": {
"id": 2,
"tag": "tag"
}
},
];
function customSort(ob1, ob2) {
if (ob1.checked > ob2.checked) {
return 1;
} else if (ob1.checked < ob2.checked) {
return -1;
}
// Else sort on the second item
if (ob1.type.id < ob2.type.id) {
return -1;
} else if (ob1.type.id > ob2.type.id) {
return 1
} else {
return 0;
}
}
console.log(array);
console.log(array.sort(customSort));
答案 2 :(得分:0)
这是使用组合排序的通用解决方案,您可以在其中将任意数量的排序条件组合为一个大型排序函数:
function to_tuples(array_of_elements) {
return array_of_elements.map((element, index) => [element, index]);
}
function to_elements(array_of_tuples) {
return array_of_tuples.map((element) => element[0]);
}
function compose_sorts(sort_functions) {
const default_sort = (l, r) => l[1] < r[1];
return sort_functions.reduceRight((master_sort, sort_function) => {
const mixin = (next_sort, left, right) => {
const result = sort_function(left, right);
return result === 0 ? next_sort(left, right) : result;
}
return mixin.bind(null, master_sort);
}, default_sort);
}
function sort_checked_to_top(left_tuple, right_tuple) {
const left = left_tuple[0];
const right = right_tuple[0];
if (left.checked !== right.checked) {
return left.checked ? -1 : 1;
}
return 0;
}
function sort_lower_id_to_top(left_tuple, right_tuple) {
const left = left_tuple[0];
const right = right_tuple[0];
if (left.type.id !== right.type.id) {
return left.type.id > right.type.id ? 1 : -1;
}
return 0;
}
const master_sort = compose_sorts([
sort_checked_to_top,
sort_lower_id_to_top,
]);
var array = [
{
"checked": true,
"name":"Name",
"type": {
"id": 1,
"tag": "tag"
}
},
{
"checked": false,
"name":"Name",
"type": {
"id": 25,
"tag": "tag"
}
},
{
"checked": true,
"name":"Name",
"type": {
"id": 3,
"tag": "tag"
}
},
{
"checked": false,
"name":"Name",
"type": {
"id": 2,
"tag": "tag"
}
},
];
const result = to_elements(to_tuples(array).sort(master_sort));
console.log(result);