简体   繁体   English

按固定值排序对象

[英]Sorting objects by fixed values

Say I have a this: 说我有这个:

["Done", "Pending", "Busy"] [“完成”,“等待”,“忙碌”]

How would I sort that so the order is busy, pending, done? 我如何排序,以便订单繁忙,待定,完成?

I will have a list of objects, each with a status value of either done, pending and busy. 我将有一个对象列表,每个对象的状态值为done,pending和busy。 So there might be 100's. 所以可能有100个。

Im not sure how this works, because its not alphabetical or logical, its based on user preference. 我不确定这是如何工作的,因为它不是按字母顺序或逻辑的,而是基于用户偏好。

I want them to always be i that order. 我希望他们永远是我的那份订单。

Im using javascript to sort the array. 我用javascript对数组进行排序。

Checksum: "e791e37b19187712fc95078e8fd2b367"
FileLastWriteTimeUtc: "2013/06/07 01:22:40 PM"
FileName: "1.jpg"
PartsSuccess: 0
PartsTotal: 0
Path: "\Image Content Tests"
Size: 4174754
Status: "Done"

Thanks 谢谢

You assign each of your values a sort order. 您可以为每个值分配排序顺序。 This sort order is a number, and entirely up to you on what to make them. 这种排序顺序是一个数字,完全取决于你做什么。 You then sort by the sort order of the value you actually want to sort. 然后按实际要排序的值的排序顺序排序。 An example: 一个例子:

var data = [
    {
        id: 1,
        state: "Pending"
    },
    {
        id: 2,
        state: "Done"
    },
    {
        id: 3,
        state: "Busy"
    }
];

var sortOrders = {
    "Done": 1,
    "Busy": 2,
    "Pending": 3
};

data.sort(function(a, b) {
    return sortOrders[a.state] - sortOrders[b.state];
});

demo: http://ideone.com/qsz52d 演示: http//ideone.com/qsz52d

Assuming your list of objects is an array, use a custom sort function: 假设您的对象列表是一个数组,请使用自定义排序函数:

sortMe.sort(function (a, b) {
    var statusA = ['Busy', 'Pending', 'Done'].indexOf(a.Status),
        statusB = ['Busy', 'Pending', 'Done'].indexOf(b.Status);
    return statusA - statusB;
});

where sortMe is something like the following: 其中sortMe类似于以下内容:

var sortMe = [{
    "Checksum": "e791e37b19187712fc95078e8fd2b367",
    "FileLastWriteTimeUtc": "2013/06/07 01:22:40 PM",
    "FileName": "1.jpg",
    "PartsSuccess": 0,
    "PartsTotal": 0,
    "Path": "\Image Content Tests",
    "Size": 4174754,
    "Status": "Done"
},{
    "Checksum": "e791e37b19187712fc95078e8fd2b367",
    "FileLastWriteTimeUtc": "2013/06/07 01:22:40 PM",
    "FileName": "2.jpg",
    "PartsSuccess": 0,
    "PartsTotal": 0,
    "Path": "\Image Content Tests",
    "Size": 4174754,
    "Status": "Busy"
},{
    "Checksum": "e791e37b19187712fc95078e8fd2b367",
    "FileLastWriteTimeUtc": "2013/06/07 01:22:40 PM",
    "FileName": "3.jpg",
    "PartsSuccess": 0,
    "PartsTotal": 0,
    "Path": "\Image Content Tests",
    "Size": 4174754,
    "Status": "Pending"
}];

I assume that you have objects like 我假设你有像这样的对象

[
{
    'name' : 'Sending mail to manager',
    'status': 'pending'
},

{
    'name' : 'Talking to girl friend',
    'status': 'busy'
},

{
    'name' : 'Breakfast',
    'status': 'done'
},
]

I suggest you to use underscorejs 我建议你使用underscorejs

_.sortBy(a, function(obj) {
    if (obj.status === 'busy') {
        return 1; 
    } else if (obj.status === 'pending') { 
        return 2; 
    }
    return 3; 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM