Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using orderBy on ng-repeat with a multidimensional array

I have a dynamic data model coming in via a websocket, which looks like this:

var results = [
    [
        {name:'A'},
        {price: 0.00}
    ],
    [
        {name:'C'},
        {price: 0.00}
    ],
]

I'm using my ng-repeat as follows:

ng-repeat="result in results"

Whenever I need to access one of the arrays within the result array I do:

result[0].name

The issue I'm having is that the orderBy filter on ngRepeat doesn't seem to allow me to do this:

ng-repeat="result in results | orderBy: result[0].name

Perhaps it's a basic misunderstanding of how Angular works, but I fail to understand why this wouldn't work. Is it incorrect syntax, or is it due to my data model being dynamic? Should I be setting up a $scope.$apply somewhere?

I've tried with quotes, and I've tried setting up a predicate in the function that parses the data initially, setting predicate to each instance of the result.name as it comes through, but this also doesn't work.

Any help is very much appreciated.

like image 340
Kyle G Avatar asked Jan 21 '15 20:01

Kyle G


1 Answers

This question is really interesting. Since orderBy will be using the current object, you have to assign the order string relatively.

This will do the trick:

ng-repeat="result in results | orderBy: 'this[0].name'
like image 144
Himmet Avsar Avatar answered Oct 30 '22 16:10

Himmet Avsar