Sorting objects in a Json without id in Angularjs

Asked

Viewed 697 times

2

How can I sort the display of a Json object array in Angular, ng-repeat? orderBy? and this json has no id to do this ordering, I would like to be able to sort it by sequence of "last added".

2 answers

1

  • Yes Guilherme, I understood about this, the problem is that I don’t have elements like ID or DATA in this api. I have only the data, if I sort by name or other element I will not have the result of "last added". I need a way to sort by record, but I don’t have an appointment for this...

  • @Fábioduarte if your insertion order is the JSON order you can create a filter simply by asking to invert!

0

Like the William spoke, you can use the filters that Angularjs itself offers you. But, since you don’t seem to have any attributes within your JSON that can be used directly to do this sort, you might want 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 you in HTML:

{{seuObjetoJSON | meuFiltroOrdenarPor}}

However, if you need to sort by "last added", I don’t see a way to solve this that doesn’t involve putting this attribute in your JSON, making every object have such an attribute.

It would help a lot if you could put an example of JSON that you are dealing with.


References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.