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:

I solved this by spliting the string into 2 parts by Space,converting the 2nd part to integer ,if it is "k" giving -1 as value and then sorting array by converted 2nd part of the string.code is given below

for ( var i = 0; i < scope.obj.length; i++) {
                var option= scope.obj[i].option.split(" ");
                scope.obj[i].optionValue = parseInt(optionValueSet(option[1]));
            }

function optionValueSet(value) {
            if (value=== "K"){
                return -1;
            } 
            return value;
        }

and my html

<a ng-repeat="item in obj | orderBy :'optionValue':false">{{item.option}}</a>

Solution 3:


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