我有20条记录

我想按Priority字段排序记录。

Priority字段具有3个值(High, Medium, Low

我想依次显示High, Medium, Low记录中的记录。

以下是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>





希望对您有所帮助。

10-07 14:35