Skip to content Skip to sidebar Skip to footer

How To Do Natural Sorting In Angularjs?

I have an object like this below: scope.obj=[{option :'option 1' },{option :'option 2' },{option :'option 3' },{option :'option k' }] and my html Copy

Solution 2:

Solution 3:

As of AngularJS 1.5.7, a comparator argument can be passed as a 4th argument to the orderBy filter. You can then tweak the comparison between elements.

controller.js

$scope.naturalCompare = function(a, b) {
  return naturalSort(a.value, b.value);
}

view.html

<spanng-repeat="item in list | orderBy: 'name' : false : naturalCompare">item.name</span>

In here we implented a shortcut in a module angularjs-naturalorderby which uses javascript-natural-sort. So you can use it like.

view.html

<spanng-repeat="item in list | naturalOrderBy: 'name'">item.name</span>

Post a Comment for "How To Do Natural Sorting In Angularjs?"