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: add a method requiring closures as the sort interface #9998
Comments
Hmm, I think it's difficult judgement for include the sorter because we can't make sure how the sorter is useful for everyone, I think. package main
import (
"fmt"
"sort"
)
type sorter struct {
FLen func() int
FLess func(int, int) bool
FSwap func(int, int)
}
func (s *sorter) Len() int {
return s.FLen()
}
func (s *sorter) Less(i, j int) bool {
return s.FLess(i, j)
}
func (s *sorter) Swap(i, j int) {
s.FSwap(i, j)
}
func main() {
fs := []struct {
v int
}{
{3}, {1}, {2},
}
sort.Sort(&sorter{
FLen: func() int { return len(fs) },
FLess: func(i, j int) bool { return fs[i].v < fs[j].v },
FSwap: func(i, j int) { fs[i], fs[j] = fs[j], fs[i] },
})
fmt.Println(fs)
} |
Here're two standard scenarios: Sorting the same array with different comparatorsIt's very inconvinient to define a type for the array of a custom type and assert it just for sorting. When you have to sort in different ways, things become worse. Sorting without changing the original arrayI.e. create an index array to represent the sorting result. Here are some code fragments showing these cases: type MyData struct {
score int
time int
}
func main() {
list := []MyData{...}
// descending sort by score
sort.Sort(len(list), func(i, j int) bool {
return list[i].score > list[i].score
}, func(i, j int) {
list[i], list[j] = list[j], list[i]
})
// sort by time
sort.Sort(len(list), func(i, j int) bool {
return list[i].time < list[i].time
}, func(i, j int) {
list[i], list[j] = list[j], list[i]
})
// sort with indexes
indexes := make([]int, len(list)
for i := range list {
indexes[i] = i
}
sort.Sort(len(list), func(i, j int) bool {
return list[indexes[i]].score < list[indexes[j]].score
}, func(i, j) {
indexes[i], indexes[j] = indexes[j], indexes[i]
})
} Closures are clearly making the code consie and much easier to maintain. PS: I think the |
If you want this, it is easy to create:
It doesn't need to be in the standard library. There is already one way to write a sort, and that's better than two. |
The method signature looks like this:
func SortF(Len int, Less func(int, int) bool, Swap func(int, int))
The could simplify some code and make it much easier to read and maintain when closures can be used.
The text was updated successfully, but these errors were encountered: