Guru
Guru

Reputation: 21

How do I sort a list alphabetically and numerically in angularjs?

Here is my scenario

<selectclass="form-control" id="my_select"><optionvalue="0">List is Number</option><option value="1">List is Alphabetical</option></select>

  <liclass="ui-state-default " ng-repeat="damageResult in damageResultList>  <span>{{damageResult.damageMechanismList}}hfgh</span></li> 

Upvotes: 1

Views: 998

Answers (2)

harishr
harishr

Reputation: 18065

what you can do is define a custom function to sort and convert the text to number in case field is number

$scope.sorterFunc = function(entity){
    return $scope.isFieldNumber? parseInt(entity[$scope.property], 10) : entity[$scope.property];
};

and then in ng-repeat

<div ng-repeat="entity in entityArray | orderBy:sorterFunc ">

Upvotes: 1

Satyam Koyani
Satyam Koyani

Reputation: 4274

Apply orderBy filter on that field. You can learn more here about sorting.

ng-repeat="damageResult in damageResultList | orderBy:'YOUR_FIELD_NAME'"

Upvotes: 4

Related Questions