Skip to content

refactor: replace sort.Slice with slices.Sort for natural ordering #2493

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yingshanghuangqiao
Copy link

There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.

@jrick
Copy link
Member

jrick commented Jun 20, 2025

There is one more sort.Slice in this file that can be replaced.

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

can be rewritten as

	slices.SortFunc(infos, func(a, b *types.GetPeerInfoResult) int {
		switch {
		case a.ID < b.ID:
			return -1
		case a.ID > b.ID:
			return 1
		default:
			return 0
		}
	})

@yingshanghuangqiao
Copy link
Author

There is one more sort.Slice in this file that can be replaced.

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

can be rewritten as

	slices.SortFunc(infos, func(a, b *types.GetPeerInfoResult) int {
		switch {
		case a.ID < b.ID:
			return -1
		case a.ID > b.ID:
			return 1
		default:
			return 0
		}
	})

Thank you for your guidance. I have made the suggested changes. Please review it again.

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

Successfully merging this pull request may close these issues.

2 participants