> Tobias, thanks for the feedback. Indeed I cannot quite do as you
> suggest, as I'm iterating over a raw object{}, not an Array(), thus I
> can't sort the obj in advance.
You can represent order and key/val relationship with a
more complex data structure. I do this all the time.
Stuff each key/val pair of your object into an array,
then use your custom sort function to compare the values of
each object in the array (
http://tobiah.org/foo.html).
<script>
var ob = {
'dog': 'canine',
'cow': 'bovine',
'horse': 'equestrian',
'cat': 'feline'
}
console.log(ob_sorter(ob));
function ob_sorter(ob){
var holder = [];
for(var x in ob){
holder.push({key: x, val: ob[x]});
}
holder.sort(val_sort);
return holder
}
function val_sort(left, right){
return left.val > right.val
}
</script>
Then you can utilize the sorted pairs in a slightly different way:
<span ng-repeat='pair in my_ob'>
{{pair.key}}{{pair.val}} or whatever you wanted to do with them.
</span>
If you want to sort by keys, just use left.key > right.key in the val_sort().
Tobiah