javascript - AngularJS - Sorting ng-repeat on string with numbers in them -
i have list of objects in table-view sort properly.
amongst other things, objects contain name-field. name field can contain numbers, example: chairman seat 1 seat 2 seat 3 seat 11 seat 12 seat 23 secretary
this sorted this: chairman seat 1 seat 11 seat 12 seat 2 seat 23 seat 3 secretary
this doesn't seem natural way of sorting list when sorting name.
now i'm using ng-repeat this: seat in seats | orderby:orderfield:orderreverse track seat.id
orderfield
variable set when clicking table header , orderreverse
reversing.
i've tried making custom filter makes sure behaves failed. seems javascript won't order unless break string. i'd rather not because data updated polling. way force leading zero's since users entering stuff manually i'm not sure if should.
also, not names numbers in them, can't cut them off
so, suggestions on how make list show normally?
edit: cleared info problem.
you can use custom sort function orderby
(it can take custom sorting function too)
define sort function in controller:
$scope.sorter = function (a){ return parseint(a.replace( /^\d+/g, '')); // gets number string }
and in template
seat in seats | orderby:sorter track seat.id
edit per modified problem statement:
do manual sorting in controller instead of ng-repeart using naturalsort
$scope.seats = [{"id": "seat 12"},{"id": "seat 3"},{"id": "seat 1"},{"id": "seat 2"},{"id": "secretary"}]; $scope.seats.sort(function(a,b) { return naturalsort(a.id, b.id); })
or check angular module http://blog.overzealous.com/post/55829457993/natural-sorting-within-angular-js , fiddle demonstrating - http://jsfiddle.net/we7h2/3/
Comments
Post a Comment