我有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。我需要按顺序。
那就是我希望首先所有高记录,然后中等记录和 atlast 低记录。
答案 0 :(得分:2)
您可以查看以下简单代码:
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>
我希望它会对你有所帮助。
答案 1 :(得分:0)
试试这个:
var data = [];
var priority = ["low", "medium", "high"];
function createData() {
for (var i = 0; i < 10; i++) {
var index = Math.floor((Math.random() * 10) % 3);
var p = priority[index];
data.push({
"index": i,
"Priority": p
});
}
}
function createLI() {
var ul = document.getElementsByTagName("ul")[0];
var li = "";
data.forEach(function(row) {
li += "<li> " + row.index + " : " + row.Priority + " </li>";
});
ul.innerHTML = li;
}
function sortList(sortOrder) {
var order = (sortOrder == 'desc') ? -1 : 1;
data.sort(function(a, b) {
var p1, p2;
priority.forEach(function(row, index) {
if (a.Priority == row)
p1 = index;
if (b.Priority == row)
p2 = index;
})
if (p1 < p2) return -1 * order;
if (p1 > p2) return 1 * order;
return 0;
});
createLI();
}
(function() {
createData();
createLI();
})()
li {
list-style-type: none;
padding: 5px;
border-bottom: 1px solid #ddd;
margin-left: -20px;
width: 100px;
}
<ul></ul>
<button onclick="sortList('asc')">Asc Sort</button>
<button onclick="sortList('desc')">Desc Sort</button>
答案 2 :(得分:0)
尝试使用String.prototype.match()
与RegExp
val + "\.\\w+(?=.*,)", "g"
匹配val
,后跟任何字符,后跟任何字母数字字符;其中val
"High"
,"Medium"
或"Low"
存储在Array.prototype.forEach()
内迭代的数组中,利用Array.prototype.concat()
展平结果数组{{1}包含从&#34;高&#34;排序的输入文本to&#34; Medium&#34;到&#34;低&#34; 。使用res
返回从&#34;高&#34;中排序的三个数组。 to&#34; Medium&#34;到&#34;低&#34;
res.push()
&#13;
答案 3 :(得分:0)
使用常量进行排序顺序的简化版本:
数据:强>
var data = [
{
name: 'Andy',
priority: 'High'
},
{
name: 'John',
priority: 'Low'
},
{
name: 'Tim',
priority: 'Medium'
},
{
name: 'Bob',
priority: 'Medium'
},
{
name: 'Lucas',
priority: 'Medium'
},
{
name: 'Mark',
priority: 'High'
},
{
name: 'Fred',
priority: 'Low'
}
];
<强>代码:强>
var SORT_ASC = -1;
var SORT_DESC = 1;
function sort_by_priority ( order ) {
var prs = ['High', 'Medium', 'Low'];
data.sort(function ( a, b ) {
var x = prs.indexOf(a.priority);
var y = prs.indexOf(b.priority);
if ( x < y ) return -1 * order;
if ( x > y ) return 1 * order;
return 0;
});
}
<强>测试强>
sort_by_priority(SORT_DESC);
console.log(data);