Add sorting
This commit is contained in:
parent
5e76002027
commit
b2314b5f63
6
main.go
6
main.go
@ -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
36
sorting/sorting.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user