Created
February 24, 2018 02:58
-
-
Save sekky0905/73b31e280734e2f236a2fc17cb12461b to your computer and use it in GitHub Desktop.
GoのSliceをSortする(sort.Sliceとsort.SliceStable) ref: https://qiita.com/Sekky0905/items/2d5ccd6d076106e9d21c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Slice(slice interface{}, less func(i, j int) bool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
people := []Person{ | |
{Name: "V", Age: 3}, | |
{Name: "K", Age: 3}, | |
{Name: "Y", Age: 3}, | |
{Name: "A", Age: 4}, | |
{Name: "E", Age: 3}, | |
{Name: "D", Age: 1}, | |
{Name: "C", Age: 3}, | |
{Name: "X", Age: 2}, | |
{Name: "B", Age: 3}, | |
} | |
sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name }) | |
fmt.Printf("NameでSort(Not-Stable):%+v\n", people) | |
sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age }) | |
fmt.Printf("AgeでSort(Not-Stable):%+v\n", people) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NameでSort(Not-Stable):[{Name:A Age:4} {Name:B Age:3} {Name:C Age:3} {Name:D Age:1} {Name:E Age:3} {Name:K Age:3} {Name:V Age:3} {Name:X Age:2} {Name:Y Age:3}] | |
AgeでSort(Not-Stable):[{Name:D Age:1} {Name:X Age:2} {Name:V Age:3} {Name:C Age:3} {Name:E Age:3} {Name:K Age:3} {Name:B Age:3} {Name:Y Age:3} {Name:A Age:4}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func SliceStable(slice interface{}, less func(i, j int) bool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
people2 := []Person{ | |
{Name: "V", Age: 3}, | |
{Name: "K", Age: 3}, | |
{Name: "Y", Age: 3}, | |
{Name: "A", Age: 4}, | |
{Name: "E", Age: 3}, | |
{Name: "D", Age: 1}, | |
{Name: "C", Age: 3}, | |
{Name: "X", Age: 2}, | |
{Name: "B", Age: 3}, | |
} | |
sort.SliceStable(people2, func(i, j int) bool { return people2[i].Name < people2[j].Name }) | |
fmt.Printf("NameでSort(Stable):%+v\n", people2) | |
sort.SliceStable(people2, func(i, j int) bool { return people2[i].Age < people2[j].Age }) | |
fmt.Printf("AgeでSort:%+v\n", people2) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NameでSort(Stable):[{Name:A Age:4} {Name:B Age:3} {Name:C Age:3} {Name:D Age:1} {Name:E Age:3} {Name:K Age:3} {Name:V Age:3} {Name:X Age:2} {Name:Y Age:3}] | |
AgeでSort:[{Name:D Age:1} {Name:X Age:2} {Name:B Age:3} {Name:C Age:3} {Name:E Age:3} {Name:K Age:3} {Name:V Age:3} {Name:Y Age:3} {Name:A Age:4}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment