按自定义订单排序[重复]

时间:2022-09-06 12:43:54

This question already has an answer here:

这个问题在这里已有答案:

I was wondering how I can sort an array on a custom order, not alphabetical. Imagine you have this array/object:

我想知道如何在自定义订单上排序数组,而不是按字母顺序排序。想象一下你有这个数组/对象:

var somethingToSort = [{
    type: "fruit",
    name: "banana"
}, {
    type: "candy",
    name: "twix"
}, {
    type: "vegetable",
    name: "broccoli"
}, {
    type: "vegetable",
    name: "carrot"
}, {
    type: "fruit",
    name: "strawberry"
}, {
    type: "candy",
    name: "kitkat"
}, {
    type: "fruit",
    name: "apple"
}];

In here we have 3 different types: fruit, vegetable and candy. Now I want to sort this array, and make sure that all fruits are first, candies come after fruits, and vegetables be last. Each type need their items to be sorted on alphabetical order. We will use a function like sortArrayOnOrder ( ["fruit","candy","vegetable"], "name" ); So basically, you would end up with this array after sorting:

在这里,我们有3种不同的类型:水果,蔬菜和糖果。现在我想对这个数组进行排序,并确保所有水果都是第一个,糖果来自水果,蔬菜是最后的。每种类型都需要按字母顺序对其项目进行排序。我们将使用sortArrayOnOrder([“fruit”,“candy”,“vegetable”],“name”)等函数;所以基本上,你会在排序后得到这个数组:

var somethingToSort = [{
    type: "fruit",
    name: "apple"
}, {
    type: "fruit",
    name: "banana"
}, {
    type: "fruit",
    name: "strawberry"
}, {
    type: "candy",
    name: "kitkat"
}, {
    type: "candy",
    name: "twix"
}, {
    type: "vegetable",
    name: "broccoli"
}, {
    type: "vegetable",
    name: "carrot"
}];

Anyone an idea how to create a script for this?

有人知道如何为此创建脚本吗?

3 个解决方案

#1


13  

Improved version of Cerbrus' code:

Cerbrus代码的改进版本:

var ordering = {}, // map for efficient lookup of sortIndex
    sortOrder = ['fruit','candy','vegetable'];
for (var i=0; i<sortOrder.length; i++)
    ordering[sortOrder[i]] = i;

somethingToSort.sort( function(a, b) {
    return (ordering[a.type] - ordering[b.type]) || a.name.localeCompare(b.name);
});

#2


2  

Try this:

尝试这个:

var sortOrder = ['fruit','candy','vegetable'];   // Declare a array that defines the order of the elements to be sorted.
somethingToSort.sort(
    function(a, b){                              // Pass a function to the sort that takes 2 elements to compare
        if(a.type == b.type){                    // If the elements both have the same `type`,
            return a.name.localeCompare(b.name); // Compare the elements by `name`.
        }else{                                   // Otherwise,
            return sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type); // Substract indexes, If element `a` comes first in the array, the returned value will be negative, resulting in it being sorted before `b`, and vice versa.
        }
    }
);

Also, your object declaration is incorrect. Instead of:

此外,您的对象声明不正确。代替:

{
    type = "fruit",
    name = "banana"
}, // etc

Use:

使用:

{
    type: "fruit",
    name: "banana"
}, // etc

So, replace the = signs with :'s.

所以,用:替换=符号。

#3


0  

Array.sort accepts a sort function where you can apply custom sorting logic.

Array.sort接受一个排序函数,您可以在其中应用自定义排序逻辑。

#1


13  

Improved version of Cerbrus' code:

Cerbrus代码的改进版本:

var ordering = {}, // map for efficient lookup of sortIndex
    sortOrder = ['fruit','candy','vegetable'];
for (var i=0; i<sortOrder.length; i++)
    ordering[sortOrder[i]] = i;

somethingToSort.sort( function(a, b) {
    return (ordering[a.type] - ordering[b.type]) || a.name.localeCompare(b.name);
});

#2


2  

Try this:

尝试这个:

var sortOrder = ['fruit','candy','vegetable'];   // Declare a array that defines the order of the elements to be sorted.
somethingToSort.sort(
    function(a, b){                              // Pass a function to the sort that takes 2 elements to compare
        if(a.type == b.type){                    // If the elements both have the same `type`,
            return a.name.localeCompare(b.name); // Compare the elements by `name`.
        }else{                                   // Otherwise,
            return sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type); // Substract indexes, If element `a` comes first in the array, the returned value will be negative, resulting in it being sorted before `b`, and vice versa.
        }
    }
);

Also, your object declaration is incorrect. Instead of:

此外,您的对象声明不正确。代替:

{
    type = "fruit",
    name = "banana"
}, // etc

Use:

使用:

{
    type: "fruit",
    name: "banana"
}, // etc

So, replace the = signs with :'s.

所以,用:替换=符号。

#3


0  

Array.sort accepts a sort function where you can apply custom sorting logic.

Array.sort接受一个排序函数,您可以在其中应用自定义排序逻辑。