我想根据以下条件对数据进行排序:
按
类型
排序,优先级为[州、城市、地区]。-类型为“state”的数据应放在第一位,然后是“city”类型的数据,依此类推。
重复数据应按
分数
排序。-例如:如果 3 行有类型,则按分数排序。
我的数据对象是:
$scope.data = [{
id: 1,
name: 'ABC',
type: 'city',
score: 0.1
}, {
id: 2,
name: 'PQR',
type: 'city',
score: 0.7
}, {
id: 3,
name: 'ABC',
type: 'state',
score: 0.3
});
模板是:
<div ng-repeat="d in data">
<span>{{d.name}}</span>
<span>{{d.type}}</span>
<span>{{d.score}}</span>
</div>
结果应该是这样的:
ABC state 0.3
PQR city 0.7
ABC city 0.1
Here是plunker的链接。
我想按多个字段以及特定优先级对其进行排序。
任何帮助将不胜感激。提前致谢。
最佳答案
//这是我的代码。我认为这段代码可以解决您的问题
var tempFlag = {'state':1, 'city':2, 'district':3}
$scope.data=[
{
id: 1,
name: 'ABC',
type: 'city',
score: 0.1
}, {
id: 2,
name: 'PQR',
type: 'city',
score: 0.7
}, {
id: 3,
name: 'ABC',
type: 'state',
score: 0.3
}
].sort(function(a, b) {
if(a.type == b.type)
return a.score < b.score;
return tempFlag[a.type] > tempFlag[b.type];
});
关于javascript - 按多个字段和条件对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44394050/