We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
package main import ( "fmt" "strings" "sort" ) func main() { family := []struct { Name string Age int }{ {"Alice", 23}, {"David", 2}, {"Eve", 2}, {"Bob", 25}, } // 用 age 排序,年龄相等的元素保持原始顺序 sort.SliceStable(family, func(i, j int) bool { return family[i].Age < family[j].Age }) fmt.Println(family) // [{David 2} {Eve 2} {Alice 23} {Bob 25}] //下面实现排序order by age asc, name desc,如果 age 和 name 都相等则保持原始排序 sort.SliceStable(family, func(i, j int) bool { if family[i].Age != family[j].Age { return family[i].Age < family[j].Age } return strings.Compare(family[i].Name, family[j].Name) == 1 }) fmt.Println(family) // [{Eve 2} {David 2} {Alice 23} {Bob 25}] }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: