Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy over simple array?

Tags:

angularjs

I have an array like this:

$scope.telcodes = ['44', '01', '221', '335'];

How do I use orderBy on a simple array like this to produce an ordered list starting from 01?

I know how to do this on an array containing objects but not on simple arrays like the above.

<li data-ng-repeat="telcode in telcodes | orderBy:????:false track by $index">
like image 230
Malcr001 Avatar asked Jan 12 '23 21:01

Malcr001


1 Answers

You can declare simple proxy function within your controller and use it:

Controller:

$scope.proxy = function(x) { return x; }

HTML:

<tr ng-repeat="t in telcodes | orderBy: proxy">

However, it gives you string comparison (demo).

To get standard numeric comparison modify proxy a little (demo):

$scope.proxy = function(x) { return x * 1; }
like image 160
MarcinJuraszek Avatar answered Jan 21 '23 10:01

MarcinJuraszek