diff --git a/main.go b/main.go index 3c1d20c..a21c216 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,8 @@ package main // import "git.sangeeth.dev/gobyexample/ratelimiting" // import "git.sangeeth.dev/gobyexample/atomics" // import "git.sangeeth.dev/gobyexample/mutex" -import "git.sangeeth.dev/gobyexample/statefulgoroutines" +// import "git.sangeeth.dev/gobyexample/statefulgoroutines" +import "git.sangeeth.dev/gobyexample/sorting" func main() { // runes.Runes() @@ -37,5 +38,6 @@ func main() { // ratelimiting.RateLimiting() // atomics.Atomics() // mutex.Mutex() - statefulgoroutines.StatefulGoroutines() + // statefulgoroutines.StatefulGoroutines() + sorting.Sorting() } diff --git a/sorting/sorting.go b/sorting/sorting.go new file mode 100644 index 0000000..d6b19ae --- /dev/null +++ b/sorting/sorting.go @@ -0,0 +1,36 @@ +package sorting + +import ( + "cmp" + "fmt" + "slices" +) + +type Person struct { + name string + age uint +} + +func Sorting() { + nums := []int{1, 3, 2, 5, 4} + slices.Sort(nums) + fmt.Printf("After sort: %v\n", nums) + fmt.Printf("Is nums sorted? %v\n", slices.IsSorted(nums)) + + slices.SortFunc(nums, func(a, b int) int { + return cmp.Compare(b, a) + }) + fmt.Printf("After sorting with SortFunc: %v\n", nums) + + peeps := []Person{ + {"Jack", 30}, + {"Depp", 32}, + {"Markovich", 28}, + } + + slices.SortFunc(peeps, func(a, b Person) int { + return cmp.Compare(a.age, b.age) + }) + + fmt.Printf("Sorted by age: %v\n", peeps) +}