我有 20 条记录
我想根据优先级
字段对记录进行排序。
优先级
字段有3个值(高、中、低
)
我想分别按顺序显示高、中、低
记录中的记录。
下面是 C#
版本 ---> 我需要在 javascript 中执行此操作
`var groupedRecords = records.OrderByDescending(x => x.Priority == "High").ThenByDescending(x > x.Priority == "Medium").ThenByDescending(x > x.Priority == "Low")`
在 JavaScript 中,
var csvRecords =priorities.split(',');
//现在我得到的是混合订单的 csv。我需要按顺序。
也就是说,我想要首先所有高记录,然后中记录和最后低记录。
最佳答案
您可以看一下下面的简单代码:
var points = [{Name: "House",Priority :"High"}, {Name: "Travel",Priority :"Medium"}, {Name: "Children",Priority :"High"}, {Name: "Personal Loan",Priority :"Low"}, {Name: "Car",Priority :"Medium"}, {Name: "Shopping",Priority :"Low"}];
document.getElementById("demo").innerHTML = JSON.stringify(points);
function myFunction() {
var priorityArray = ["High", "Medium","Low"]
points.sort(function(a, b){
var firstPrio = priorityArray.indexOf( a.Priority) ;
var secPrio = priorityArray.indexOf(b.Priority)
return firstPrio -secPrio
});
document.getElementById("sortedArray").innerHTML = JSON.stringify(points);
}
<p>Click the button to sort the array.</p>
<button onclick="myFunction()">Sort it</button>
<br/><br/>
Original Array:
<p id="demo"></p>
Sorted Array:
<p id="sortedArray"></p>
希望对你有一点帮助。
关于javascript - 如何在 Javascript 中对项目类别进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909443/