Custom orderby funcition

Asked

Viewed 39 times

0

I need to create a function to make a ordeBy customized into a ngRepeat, but I am not able to think logical that I should use to perform the ordering.

The requirement to sort the list is as follows:

Resources must be displayed in ascending order of the date of presentation. Objections must be displayed below the resource associated in ascending order of the date of submission.

Follow an example array, where the association between Resources and Challenges is given by the Number field:

[{
'numero': '1',
'dataApresentacao': '2016-01-26T00:00:00-02:00',
'tipo': 'RECURSO'
}, {
'numero': '2',
'dataApresentacao': '2016-01-26T00:00:00-02:00',
'tipo': 'RECURSO'
}, {
'numero': '3',
'dataApresentacao': '2016-01-28T00:00:00-02:00',
'tipo': 'RECURSO'
}, {
'numero': '1',<br>
'dataApresentacao': '2016-01-28T00:00:00-02:00',
'tipo': 'IMPUGNACAO'
}, {
'numero': '4',
'dataApresentacao': '2016-01-27T00:00:00-02:00',
'tipo': 'RECURSO'
}, {
'numero': '1',
'dataApresentacao': '2016-01-29T00:00:00-02:00',
'tipo': 'IMPUGNACAO'
}, {
'numero': '2',
'dataApresentacao': '2016-01-29T00:00:00-02:00',
'tipo': 'IMPUGNACAO'
}]

Any idea?

  • Guillherme, when formatting your question, use the markdown made available by the website.

  • Did you try anything? It’s more productive to help you if we can analyze your code, so you can give more efficient answers and you learn more in the process. Good luck!

1 answer

0

The answer is: you don’t need a orderBy customized.

The orderBy accepts more than one field to be ordered, reverse or not.

Example:

<div ng-repeat="dados in listaArray | orderBy:['primeiro','segundo']"> </div>

For your method, you can use something similar:

<div ng-repeat="dados in listaArray | orderBy:['dataApresentacao','numero']"> </div>

Remembering that to reverse the order, just add the negative sign to the field name, example: -dataApresentacao

  • Thanks for the tip @Celsomtrindade ! But it didn’t work out, here is the link to check: https://jsfiddle.net/a2a2uLjx/12/ The items of type Challenge number 4 and 5 should be below the items of type Resource number 4 and 5 susceptibly.

  • But you will not be able to do that if you want to keep order by date. This way it is ordering first by date. And then it sorts by type. That is, within the values of the date 28, the values of the type Resource will be ahead of the type of objection, see: https://jsfiddle.net/a2a2uLjx/13/ This is unfortunately a limitation that has no way around, because you must either organize by date, or by name, or by name within that single date, do you understand? See a more elaborate example: https://jsfiddle.net/a2a2uLjx/14/

Browser other questions tagged

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