expr-lang/expr

Sort issue

Closed this issue · 3 comments

Hello @antonmedv !

First of all thanks for this one of the best tool which i know!

Here my problem(i am not sure is it problem or suggestion).

I trying sort array here https://expr-lang.org/playground:

sort(Posts) // or sort(Posts, 'desc')

From my point of view like user i am expect results should array with same order/reverse order here the error (as i understand no way to do that right now):

cannot deref <invalid reflect.Value> (3:1)
 | sort(Posts)
 | ^

My suggestion is: create new function which will be do that: sortByIndex(array, order = 'asc|desc') or create reverse. I am prefer for reverse(but may be need support string as well)

Examples:

sortByIndex([3,2,1)) == [3,2,1]
sortByIndex([3,2,1), 'desc') == [1,2,3]
reverse([3,2,1]) == [1,2,3]
reverse(reverse([3,2,1])) == [3,2,1]

I can create pull request with this function

Yes, sort() is just for array on numbers, and sortBy() is for array structs/maps.

sortBy(Posts, 'Title') | map(.ID)

And sure, lets add reverse().

Adding MR: #553

Added #558