6

Let's say I have this object:

var rows = [
    [1, 2, 3, 4],
    [11, 222, 3333, 4444]
];

Given that, and this template:

<tr ng-repeat="row in rows | orderBy ????">
    <td ng-repeat="cell in row">{{ cell }}</td>
</tr>

...how can I order an ng-repeat by the second "column" of each row (the value at index 1 of the given row element)?

Am I correct that Angular does not support this case—without having to write a custom sort function? (I'm just prototyping, so using ng-init to define my scope variables instead of creating a controller.)

2 Answers 2

3

Actually it does. You can create custom order by functions.

http://plnkr.co/edit/f6hCbHcLkrjyTODvDWjM?p=preview

<div ng-repeat="row in rows | orderBy:secondIndex">
    {{row}}
</div>

//In controller
$scope.secondIndex = function(arr){
    return arr[1];
}
Sign up to request clarification or add additional context in comments.

Comments

2

You just should to use orderBy:'1':

<tr ng-repeat="row in rows | orderBy:'1'">
   <td ng-repeat="cell in row">{{ cell }}</td>
</tr>

Demo

2 Comments

Winner winner, chicken dinner!
Great. But I don't understand why that orderBy value is string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.