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

affected/package: sort #61222

Closed
LargeOrange opened this issue Jul 7, 2023 · 7 comments
Closed

affected/package: sort #61222

LargeOrange opened this issue Jul 7, 2023 · 7 comments

Comments

@LargeOrange
Copy link

What version of Go are you using (go version)?

$ go version
$ go version go1.17.13 darwin/arm64

Does this issue reproduce with the latest release?

I dont know

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/sundacheng/Library/Caches/go-build"
GOENV="/Users/sundacheng/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/sundacheng/go/pkg/mod"
GONOPROXY="git.ddxq.mobi"
GONOSUMDB="git.ddxq.mobi"
GOOS="darwin"
GOPATH="/Users/sundacheng/go"
GOPRIVATE="git.ddxq.mobi"
GOPROXY="https://goproxy.cn"
GOROOT="/Users/sundacheng/sdk/go1.17.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/sundacheng/sdk/go1.17.13/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.13"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/sundacheng/GolandProjects/study/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/87/y0yp0xd51tx38ddkg7vrxx8x0zr3tr/T/go-build2063815648=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

This is my code:

func main() {
	s1 := []byte("tan")
	s2 := []byte("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)
}

This is my code output:

[116 110 97] [110 97 116]

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?

[116 110 97] [110 97 116]
@LargeOrange
Copy link
Author

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

@LargeOrange
Copy link
Author

Not a bug

why?

@LargeOrange
Copy link
Author

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

@rittneje
Copy link

rittneje commented Jul 7, 2023

@LargeOrange As @Nasfame has pointed out, you wrote the callback function for the second call to sort.Sort incorrectly.

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.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2023
@golang golang locked and limited conversation to collaborators Jul 6, 2024
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

5 participants
@ianlancetaylor @gopherbot @LargeOrange @rittneje and others