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

proposal: add reverse functionality to sort.Slice #19174

Closed
ston1th opened this issue Feb 18, 2017 · 2 comments
Closed

proposal: add reverse functionality to sort.Slice #19174

ston1th opened this issue Feb 18, 2017 · 2 comments

Comments

@ston1th
Copy link

ston1th commented Feb 18, 2017

Hi gophers,

I would like to add the sort.Reverse functionality to the sort.Slice functions.

Example code:

type Less func(i, j int) bool

func ReverseLess(f Less) Less {
        return func(i, j int) bool {
                return f(j, i)
        }
}

Is this something that belongs in the stdlib or should people implement this themselves if needed?

@vcabbage
Copy link
Member

What benefit does this bring over simply reversing the comparison operator?

Sort ascending:

sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })

Sort descending:

sort.Slice(people, func(i, j int) bool { return people[i].Name > people[j].Name })

@bradfitz
Copy link
Contributor

This was already discussed in the long thread at #16721 in which the current Go 1.8 API was decided upon. e.g. #16721 (comment) proposed sort.Slice integration with reverse and Russ's reply at #16721 (comment) and other posts there.

@golang golang locked and limited conversation to collaborators Feb 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants