CrazyProgrammer
CrazyProgrammer

Reputation: 544

Order string as number in angular

using $filter in controller to sort list of json data by price and added date, in the json data price is stored as string instead of number. I want to sort them by price first then added date, sample angular code in jsfiddle

http://jsfiddle.net/5wkvzbgt/7/

$scope.results = $filter('orderBy')($scope.results, ['-price','added']);

Upvotes: 0

Views: 72

Answers (1)

Dario
Dario

Reputation: 6280

You can pass a function to your expression argument and use it to parse the string to a number. Setting the 3rd parameter to true reverse the results:

$scope.results = $filter('orderBy')($scope.results, 
                             [function(a){ return parseInt(a.price); },'-added']);

See updated fiddle

Upvotes: 2

Related Questions