Sort column number in the string

时间:2015-07-28 16:01:05

标签: angularjs

I have a question about sort the string in Angular.

The ideal sequence:
"1" q1
"1.1" q2
"1.1.1" q3
"10" q4
"11" q5

Sorted sequence after orderby
"1" q1
"10" q4
"11" q5
"1.1" q2
"1.1.1" q3

How to use the order by to have the right sequence with hierarchy, or custom function is needed? Thanks

2 个答案:

答案 0 :(得分:0)

If the list is something like <p ng-repeat="x in list">"{{x.number}}"{{x.qValue}} </p> then all you need to do is add an orderBy filter so it would look like:

<p ng-repeat="x in list| orderBy: 'number'">"{{x.number}}"{{x.qValue}} </p>

答案 1 :(得分:0)

Check working demo: JSFiddle.

Apply this sorting function to the array:

function sortByNumber(prev, curr) {
    var p = prev.index.replace(/\./g, ' ');
    var c = curr.index.replace(/\./g, ' ');
    return p > c;
};

HTML:

<div ng-app="Joy" ng-controller="JoyCtrl">
    <li ng-repeat="n in data">{{ n.index }} - {{ n.content }}</li>
</div>

For your reference: Custom sort function in ng-repeat