How can I order the display of an array of Json objects in Angular, ng-repeat
? orderBy
? json has no id to do this sort order, I would like to be able to sort it by "last added" string.
How can I order the display of an array of Json objects in Angular, ng-repeat
? orderBy
? json has no id to do this sort order, I would like to be able to sort it by "last added" string.
The best way is to use an Angular feature called "filter", it allows you to manipulate a value, you can use "order by" for this, something like:
{{item in items | orderby : 'date' }}
Here's a StackOverflow topic on the subject: link
As the Guilherme spoke , you can use the filters that AngularJS itself offers you. But since you do not seem to have any attributes within your JSON that can be used directly to do this sorting, it may be best to create your own sort filter:
app.filter('meuFiltroOrdenarPor', function(){
return function(array, atributo) {
// sua função de sort para ordenar o "array" considerando "atributo"
return array;
}
});
and to call it in HTML:
{{seuObjetoJSON | meuFiltroOrdenarPor}}
However, if you need to sort by "last added", I do not see a way to solve this that does not involve putting this attribute in your JSON, make every object have such an attribute.
It would help a lot if you could put an example of the JSON you're dealing with.
References :