Skip to content
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 for generators #38328

Open
simonschoelly opened this issue Nov 6, 2020 · 5 comments · Fixed by #46104
Open

sort for generators #38328

simonschoelly opened this issue Nov 6, 2020 · 5 comments · Fixed by #46104
Labels
sorting Put things in order

Comments

@simonschoelly
Copy link

I noticed that sort does not work for Base.Generator. Would it make sense to have an implementation for that, something like

sort(generator::Base.Generator; kwargs...) = sort!(collect(generator); kwargs...)

?

@KlausC
Copy link
Contributor

KlausC commented Nov 9, 2020

You can also use this: https://github.com/KlausC/GeneratorArrays.jl

julia> using GeneratorArrays

julia> sort(array(x^3 for x in -3:3), rev = true)
7-element Vector{Int64}:
  27
   8
   1
   0
  -1
  -8
 -27

@StefanKarpinski
Copy link
Member

I think this was one of @ararslan's first undertakings, but it never got merged. I'd be very much in favor though: it collects and then sort!s the collected result.

@LilithHafner LilithHafner added the sorting Put things in order label Jul 19, 2022
@LilithHafner
Copy link
Member

@ararslan's PR was closed with "Unless there's someone who's really into this idea, I'll go ahead and close this PR".

Now that there appears to be someone who's really into this idea (@simonschoelly & @StefanKarpinski), I've made a similar PR

@PallHaraldsson
Copy link
Contributor

Should this be reopened since the PR was reverted, or simply not add this feature? People have done without it and can rather do collect?

@KristofferC KristofferC reopened this Nov 17, 2023
@gbaraldi
Copy link
Member

gbaraldi commented Jan 18, 2024

During triage this was discussed (again), and the consensus seemed to be that the default behaviour should be collect + sort!. But that is just a reasonable default and that the documented behavior is to return something that supports getindex and iterate. So if some type wants to define a specialized return type they are still allowed to, i.e ranges returning a range. This behaviour is a bit similar to what map does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sorting Put things in order
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants