New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sort function #651
Comments
Since Elvish uses strings for numbers, a ~> put 2 100 | sort-as number
▶ 2
▶ 100
~> put 2 100 | sort-as string
▶ 100
▶ 2
~> sort-as number [2 100] # also take list argument, like each
▶ 2
▶ 100 |
This works fine for strings and numbers, but how about structured types? One idea would be to allow the specification of a comparator function which returns whether two elements are in the correct order. For example:
|
If |
Yes, the most generic solution is to use functions as comparators. On the other hand defining functions as comparators can be a bit verbose, and I like the idea of making a mini-DSL for specifying how things should be ordered, Let's make two functions, the one that takes a comparator named Why not just one function? It is doable now, because strings and lists are not callable, so comparator functions and order DSLs are disjoint. But it might be possible that we make strings or lists callable in future, and it might make sense for |
@xiaq makes sense. I think the order-DSL should be sufficient for most cases, but the generic function would be nice for more complex ones. |
@xiaq just a gentle ping on this topic, since I've recently wished we had it :) |
Now that Elvish has a dedicated number type, the |
I'm not sure I understand - you would still need a function that receives two elements of the list to sort, and tell which one should go first, no? Number comparison would allow sorting lists of numbers natively, but for strings or structured data you still need a sorting criteria function. |
Well, |
Hmm, this needs another name since |
How about |
Sounds good. |
I can live with |
I don't have strong feelings between I fully agree with the need to be able to specify at least a comparison function (maybe a key as a special case). With respect to the reverse option - I think it would be much better to have a separate |
A |
The name |
Yes, but that is mitigated, somewhat, by the well established pattern for commands that implement boolean predicates to have a |
I don't find the argument that |
It would be nice to have a sorting function in Elvish (maybe named something else, to avoid conflicting with the standard Unix
sort
command). Ideally it would support structured data, and allow specifying a sorting function (like Clojure's sort-by).The text was updated successfully, but these errors were encountered: