-
-
Notifications
You must be signed in to change notification settings - Fork 481
Description
I have an array (or slice) of struct with multiple properties like this:
type Data []struct{
key1 string
key2 string
counter int
}
Now I would like to sort this inside of expr-lang by multiple properties, some ascending, some descending. In SQL I would do something like this: ... ORDER BY counter DESC, key1 ASC, key2 ASC.
I tried with nested sortBy like this: sortBy(sortBy(sortBy(Data, .key2, "asc"), .key1, "asc"), .counter, "desc") but this did not work (I am not sure about the reason). Therefore I ended up implementing my own custom function, which I called multiSortBy, which I can call like this: multiSortBy(Data, ".counter", "desc", ".key1", "asc", ".key2", "asc"). This comes with the limitation, that in user provided functions, it is not possible (to my knowledge) to use predicates as it is in the built in functions, which limits the possibilities with my implementation of multiSortBy.
Therefore I propose to introduce a new function to expr-lang, which allows to sort by multiple criteria.
(If I missed something about sorting by multiple columns with the already present functionality, I would love to learn about those alternatives.)