Boaris
Boaris

Reputation: 5226

Angular order by key ng-repeat

I have the next map:

{
    1: [
        "John": 123,
        "Doe": 234 
    ],
    2: [
        "John": 456,
        "Doe": 345   
    ]
    //and so on...
}

I have to display them with ng-repeat sorted by index and then with inner ng-repeat sorted by value:

  • 2
    • Doe 234
    • John 123
  • 1
    • John 456
    • Doe 345

I tried to find the solution but didn't find anyting.

Upvotes: 0

Views: 358

Answers (1)

Łukasz
Łukasz

Reputation: 2171

It's easy to order by key when you use angular-underscore

<div ng-repeat="item in pairs(obj) | orderBy:first:true"> Key: {{ item[0] }}; Value: {{item[1]}}</div>

In your case it can look like this:

HTML:

<ul ng-repeat="obj in pairs(data) | orderBy:first:true">

  <li>{{ obj[0] }}</li>

  <ul ng-repeat="item in pairs(obj[1]) | orderBy:last:true">
       <li> {{ item[0] }} {{item[1]}}</li>
  </ul>

</ul>

JS:

$scope.data = {
  1:{
     'John':123,
     'Doe':234
    },
  2:{
     'John':456,
     'Doe':345
    }};

See working Plunker with your case

To reverse order just change orderBy:first:true to orderBy:first:false.

Upvotes: 1

Related Questions