angularjs ng-repeat 按数字顺序对字符串数组进行排序 [英] angularjs ng-repeat ordering string arrays in numerical order
问题描述
我有一个带有 ID 的帖子列表,例如1,2,3,4,5,6,7,8,9,10,11,12
(json格式)id 是字符串类型.但我想按照它的数字顺序排序正常 orderBy:'fiche.id' 列表显示如下.1,10,11,12,2,3,4,5,6,7,8,9
I have a list of post with ids like
1,2,3,4,5,6,7,8,9,10,11,12
(json format)
The ids are of type String. But I would like to order it according to its numerical order
Normal orderBy:'fiche.id' the list is displayed as below.
1,10,11,12,2,3,4,5,6,7,8,9
我希望输出是有序的 - 1,2,3,4,5,6,7,8,9,10,11,12
I would like the output to be of order - 1,2,3,4,5,6,7,8,9,10,11,12
推荐答案
我猜你的 id 是字符串.一个简单的解决方案是使用否.操作里面order by.因此,不要使用 orderBy:'id'
而是使用 orderBy:'id*1'
这会将 id 视为 no.并按照数字顺序排序.
I guess your ids are strings. A simple solution would be use a no. operation inside order by. So instead ofr orderBy:'id'
just use orderBy:'id*1'
this will consider the id as no. and sort it according to the numerical order.
这就是你所需要的 - <div ng-repeat="fiche in fiches | orderBy:'id*1'">{{fiche.id}}</div>
This is all you need - <div ng-repeat="fiche in fiches | orderBy:'id*1'">{{fiche.id}}</div>
演示:http://plnkr.co/edit/MSrPTZQP49F7Nzwhv6ef?p=preview
这篇关于angularjs ng-repeat 按数字顺序对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!