best way to sort an array of objects with two conditions
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
Answers
-
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!
-
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?
-
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.
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback