best way to sort an array of objects with two conditions

Options

I have an array of objects and i need to sort it with two fields object.generated.apr ascending and object.generated.rewards descending both are number decimal

this is what i did

the point is that the sorting must be done at the same time and not first with one and then with the other, which would remove the first

Tagged:

Answers

  • Lefteris - blupry.com
    Options

    Hey @nicola,

    If I did got this right, what you are trying to do, cannot really be done.

    A table can only be sorted based on one value (let’s say this is going to be apr), and it can have a secondary sorting value that will only be “utilised” in case an object has the same apr value.

    So one way is to check which of them have similar values and re sort them in a second operation.

    Also in case these are table specific fields, there is a much easier way to do this, by taking advantage of the indexing Xano offers. If you need more details on that don’t hesitate to ask!

  • nicola
    nicola Member
    Options

    thanks! @Lefteris - buynocodeapps.com

    these values are calculated and are not saved in the database so I cannot use sorting in the query as below, I would like to replicate the native sorting as below but on an array of objects, you say that it cannot be done and therefore the second sort Is it just in case the first one can't be applied?

  • Lefteris - blupry.com
    Options

    hi again @nicola,

    So yes, my suggestion is to iterate over the sorted table, search for those that have the same primary value, add them to a different array, and at the end you will just connect multiple arrays into a one final array with primary and secondary sorting.