$scope.units 为我提供了类似的数据
<小时/>[{"name":"unit 12: Abc","id":"K09A"},{"name":"Unit 4: Xyz","id":"C9J5"},{"name":"Unit 1: Acx","id":"X4C8"},{"name":"unit 4: Kxc","id":"W230"}, .....]
我使用过滤器来调用 ng-repeat 中的名称
ng-repeat="unit in units | orderBy: 'name'"
<小时/>
但它给了我顺序
<小时/>Unit 1 , 10 , 11, 12, 2, 3, 4, 5, ..
我想我需要解析什么的。但我无法解决这个问题。我可以进行哪些更改才能使其按整数顺序排列。
最佳答案
您需要为 orderBy
过滤器创建一个 getter function
,如下所示:
Controller :
$scope.unitOrder = function(unit){ return parseInt(unit.name.split(' ')[1]);}
查看:
ng-repeat="unit in units|orderBy:unitOrder"
Working Example
关于javascript - 对字符串数字进行排序,angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305837/