我有一个 Ex 的数据集:
$scope.friends =
[{name:'John', score:'10'},
{name:'Mary', score:'19'},
{name:'Mike', score:'-21'},
{name:'Adam', score:'-35'},
{name:'Julie', score:'29'}];
}]);
我使用此数据作为ng-repeat
的来源,
<tr ng-repeat="friend in friends | orderBy:'-score' ">
<td>{{friend.name}}</td>
<td>{{friend.score}}</td>
</tr>
我想根据分数
以降序
顺序对 friend
进行排序。就像下面这样,
Name Score
-------------
Julie 29
Mary 19
John 10
Mike -21
Adam -35
但我得到的输出为,
Name Score
-------------
Julie 29
Mary 19
John 10
Adam -35
Mike -21
这是一个Demo Plunker
请注意 -21 和 -35 在输出中的顺序不正确,这是因为 score
属性是 String
值(value)。如果我将其更改为 int 值,那么所有内容都会按预期工作。如何克服这个问题,请考虑我无法更改 score
属性的类型。
最佳答案
怎么样
<tr ng-repeat="friend in friends | orderBy:'+-score' ">
<td>{{friend.name}}</td>
<td>{{friend.score}}</td>
</tr>
关于Angularjs orderBy 与整数字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29700353/