1

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

Steve
  • 5
  • 4

2 Answers2

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>
cbender
  • 2,231
  • 1
  • 14
  • 18
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

Community
  • 1
  • 1
Joy
  • 9,430
  • 11
  • 44
  • 95
  • Thanks Joy, it works fine with the array. But if it is a property in the array like $scope.data = [{IndexNumber:"1.1.1", Content:"q1"},{IndexNumber: "11",Content:"q2"}...], how to order that? – Steve Jul 28 '15 at 19:44