Hese
Hese

Reputation: 290

angular ng-repeat (key, value) | order by value

Is it possible to order an array like this:

ng-repeat="(key, value) in sets | orderBy: value['ordering']"

This is my array:

[{
"item 1": {
  "type": "text",
  "value": "1",
  "ordering": 1
},
"item 2": {
  "type": "text",
  "value": "4",
  "ordering": 3
},
"item 3": {
  "type": "text",
  "value": "8",
  "ordering": 2
}
}]

It should output: Item 1, Item 3, Item 2

EDIT: I need the key also

Upvotes: 0

Views: 1239

Answers (2)

charlietfl
charlietfl

Reputation: 171690

You are misinterpreting what you have as an array.

Your array consists of one big object and you can't order an object in javascript. Fix structure so you have array of multiple objects.

[{
  "item": 1,
  "type": "text",
  "value": "1",
  "ordering": 1
}, {
  "item": 2,
  "type": "text",
  "value": "4",
  "ordering": 3
}, {
  "item": 3,
  "type": "text",
  "value": "8",
  "ordering": 2
}]

Then you can use regular array repeater

ng-repeat="set in sets | orderBy: 'ordering'"

Upvotes: 2

Aurélien Thieriot
Aurélien Thieriot

Reputation: 5923

I you don't need the key, you might wan't to give a try to this:

https://github.com/fmquaglia/ngOrderObjectBy

Upvotes: 0

Related Questions