Skip to content

Proposal: sort by multiple properties in an array #933

Description

@breml

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions