Add sorting

This commit is contained in:
Sangeeth Sudheer 2024-05-01 18:19:36 +05:30
parent 5e76002027
commit b2314b5f63
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 40 additions and 2 deletions

View File

@ -17,7 +17,8 @@ package main
// import "git.sangeeth.dev/gobyexample/ratelimiting" // import "git.sangeeth.dev/gobyexample/ratelimiting"
// import "git.sangeeth.dev/gobyexample/atomics" // import "git.sangeeth.dev/gobyexample/atomics"
// import "git.sangeeth.dev/gobyexample/mutex" // 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() { func main() {
// runes.Runes() // runes.Runes()
@ -37,5 +38,6 @@ func main() {
// ratelimiting.RateLimiting() // ratelimiting.RateLimiting()
// atomics.Atomics() // atomics.Atomics()
// mutex.Mutex() // mutex.Mutex()
statefulgoroutines.StatefulGoroutines() // statefulgoroutines.StatefulGoroutines()
sorting.Sorting()
} }

36
sorting/sorting.go Normal file
View File

@ -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)
}