Sorting objects in a Json without id in Angularjs

2

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.

    
asked by anonymous 16.07.2015 / 15:17

2 answers

1

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

    
16.07.2015 / 15:31
0

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 :

21.02.2016 / 13:37