英文:
Typescript sorting list by status (string value)
问题
我需要按照状态对项目列表进行排序,但我不确定应该使用哪种方法,是创建比较器还是某种排序技巧?
是否可以在这里使用 lodash?
示例输入列表:
[
{id: 1, name: "name1", status: "Failed"},
{id: 2, name: "name2", status: "Success"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
]
示例排序后的列表(输出)按照 Failed -> In progress -> Reserved -> Pending -> Success 排序:
[
{id: 1, name: "name1", status: "Failed"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"},
{id: 2, name: "name2", status: "Success"}
]
我使用的堆栈是 Angular + Typescript。
编辑:列表通常很大,大约有~15k行。
英文:
I need to sort list of items by its status and I am not sure which approach I should go, is it creating comparer or some kind of sorting technique ?
Would lodash work here ?
example input list:
[
{id: 1, name: "name1", status: "Failed"},
{id: 2, name: "name2", status: "Success"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
]
example sorted list (output) sorted in Failed -> In progress -> Reserved -> Pending -> Success
[
{id: 1, name: "name1", status: "Failed"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
{id: 2, name: "name2", status: "Success"},
]
stack I am using is Angular + Typescript.
Edit: List is kinda big, about ~15k rows usually.
答案1
得分: 9
你可以根据另一个数组中的值对你的数组进行排序:
let sortOrder = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success'];
let array = [
{ id: 1, name: "name1", status: "Failed" },
{ id: 2, name: "name2", status: "Success" },
{ id: 3, name: "name3", status: "In progress" },
{ id: 4, name: "name4", status: "Reserved" },
{ id: 5, name: "name5", status: "Pending" }
];
array.sort((a, b) => {
return sortOrder.indexOf(a.status) - sortOrder.indexOf(b.status);
});
console.log(array);
英文:
You can sort your array based on values in other array:
let sortOrder = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success'];
let array = [
{ id: 1, name: "name1", status: "Failed" },
{ id: 2, name: "name2", status: "Success" },
{ id: 3, name: "name3", status: "In progress" },
{ id: 4, name: "name4", status: "Reserved" },
{ id: 5, name: "name5", status: "Pending" }
];
array.sort((a, b) => {
return sortOrder.indexOf(a.status) - sortOrder.indexOf(b.status);
});
console.log(array);
答案2
得分: 0
你可以使用以下代码中的 sort()
来根据状态对数组进行排序。
const data = [
{ id: 1, name: "name1", status: "Failed" },
{ id: 2, name: "name2", status: "Success" },
{ id: 3, name: "name3", status: "In progress" },
{ id: 4, name: "name4", status: "Reserved" },
{ id: 5, name: "name5", status: "Pending" }
]
function sortBy(data: any): any {
const order = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success']
var ordering: any = {};
for (var i = 0; i < order.length; i++)
ordering[order[i]] = i;
var result = data.sort(function(a: any, b: any) {
return (ordering[a.status] - ordering[b.status]) || a.status.localeCompare(b.status);
});
return result;
}
console.log(sortBy(data));
查看示例 https://repl.it/repls/KnowingRareNlp
英文:
You can sort using sort()
as below to sort the array using status.
const data = [
{ id: 1, name: "name1", status: "Failed" },
{ id: 2, name: "name2", status: "Success" },
{ id: 3, name: "name3", status: "In progress" },
{ id: 4, name: "name4", status: "Reserved" },
{ id: 5, name: "name5", status: "Pending" }
]
function sortBy(data: any): any {
const order = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success']
var ordering: any = {};
for (var i = 0; i < order.length; i++)
ordering[order[i]] = i;
var result = data.sort(function(a: any, b: any) {
return (ordering[a.status] - ordering[b.status]) || a.status.localeCompare(b.status);
});
return result;
}
console.log(sortBy(data));
Checkout the example https://repl.it/repls/KnowingRareNlp
答案3
得分: 0
要按自定义顺序对数据进行排序,您可以首先对项目进行分组,然后根据您的排序顺序将它们添加到最终输出中。
尝试这样做:
constructor() {
var groupedData = this.groupBy(this.data, "status");
var sortArrangement = ["Failed", "In progress", "Reserved", "Pending", "Success"]
sortArrangement.forEach(item => {
groupedData[item].forEach(x => {
this.result.push(x)
})
})
console.log(this.result)
}
groupBy(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
注意:您也可以将此逻辑移到一个管道中。
英文:
To sort the data in custom order, you can group the items first and then based on your sort order, add them to the final output.
Try like this:
constructor() {
var groupedData = this.groupBy(this.data, "status");
var sortArrangement = ["Failed", "In progress", "Reserved", "Pending", "Success"]
sortArrangement.forEach(item => {
groupedData[item].forEach(x => {
this.result.push(x)
})
})
console.log(this.result)
}
groupBy(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
Note: You can move this logic to a pipe as well.
答案4
得分: 0
要按任意值排序,创建一个映射或对象,为每个状态分配数值权重,然后使用 Array.sort()
或 lodash 的 _.orderBy()
,根据状态从映射中获取权重:
const arr = [
{"id": 1, "name": "name1", "status": "Failed"},
{"id": 2, "name": "name2", "status": "Success"},
{"id": 3, "name": "name3", "status": "In progress"},
{"id": 4, "name": "name4", "status": "Reserved"},
{"id": 5, "name": "name5", "status": "Pending"}
];
const order = new Map([
['Failed', 0],
['In progress', 1],
['Reserved', 2],
['Pending', 3],
['Success', 4]
]);
const result = _.orderBy(arr, o => order.get(o.status));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
英文:
To sort by an arbitrary value, create a Map or an object that gives numeric weight to each status, and then use Array.sort()
or lodash's _.orderBy()
, and get the weight from the map according to the status:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr = [{"id":1,"name":"name1","status":"Failed"},{"id":2,"name":"name2","status":"Success"},{"id":3,"name":"name3","status":"In progress"},{"id":4,"name":"name4","status":"Reserved"},{"id":5,"name":"name5","status":"Pending"}]
const order = new Map([['Failed', 0], ['In progress', 1], ['Reserved', 2], ['Pending', 3], ['Success', 4]])
const result = _.orderBy(arr, o => order.get(o.status))
console.log(result)
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论