Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-repeat ordering string arrays in numerical order

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

I would like the output to be of order - 1,2,3,4,5,6,7,8,9,10,11,12

like image 648
zelocalhost Avatar asked Mar 26 '26 05:03

zelocalhost


1 Answers

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.

This is all you need - <div ng-repeat="fiche in fiches | orderBy:'id*1'">{{fiche.id}}</div>

Demo: http://plnkr.co/edit/MSrPTZQP49F7Nzwhv6ef?p=preview

like image 179
guru Avatar answered Mar 28 '26 19:03

guru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!