-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
affected/package: sort #61222
Labels
Comments
func main() { s1 := strings.Split("tan", "") s2 := strings.Split("nat", "") sort.Slice(s1, func(i, j int) bool { return s1[i] > s1[j] }) sort.Slice(s2, func(i, j int) bool { return s1[i] > s1[j] }) fmt.Println(s1, s2) } output [t n a] [n a t] Why is it not sorted correctly here |
why? |
func main() { s1 := []rune("eat") s2 := []rune("tea") sort.Slice(s1, func(i, j int) bool { return s1[i] > s1[j] }) sort.Slice(s2, func(i, j int) bool { return s2[i] > s2[j] }) fmt.Println(s1, s2) } This case can be sorted correctly. But fisrt case can not |
@LargeOrange As @Nasfame has pointed out, you wrote the callback function for the second call to You wrote this: sort.Slice(s2, func(i, j int) bool {
return s1[i] > s1[j]
}) You meant to write this: sort.Slice(s2, func(i, j int) bool {
return s2[i] > s2[j]
}) And indeed, the corrected code works as expected. https://go.dev/play/p/XOAfmwboMiH Hence not a bug. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I dont know
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
This is my code:
This is my code output:
What did you expect to see?
I think s1 and s2 should get the same result after sort. But the results are different and not in order
What did you see instead?
The text was updated successfully, but these errors were encountered: