Skip to content

Commit acb7891

Browse files
committed
43: Sorting by custom compare functions
1 parent 9e9df29 commit acb7891

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ go run . -module closingchannels
6262
* [40 - Mutexes](mutexes/main.go)
6363
* [41 - Stateful Goroutines](statefulgoroutines/main.go)
6464
* [42 - Sorting](sorting/main.go)
65+
* [43 - SortingByFunctions](sortingbyfunctions/main.go)

main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/symonk/learning-golang/interfaces"
2424
"github.com/symonk/learning-golang/maps"
2525
"github.com/symonk/learning-golang/methods"
26+
"github.com/symonk/learning-golang/mutexes"
2627
"github.com/symonk/learning-golang/nonblockingchannelops"
2728
"github.com/symonk/learning-golang/pointers"
2829
"github.com/symonk/learning-golang/rangeoverchannels"
@@ -31,6 +32,9 @@ import (
3132
"github.com/symonk/learning-golang/recursion"
3233
"github.com/symonk/learning-golang/selects"
3334
"github.com/symonk/learning-golang/slices"
35+
"github.com/symonk/learning-golang/sorting"
36+
"github.com/symonk/learning-golang/sortingbyfunctions"
37+
"github.com/symonk/learning-golang/statefulgoroutines"
3438
"github.com/symonk/learning-golang/stringsrunes"
3539
"github.com/symonk/learning-golang/structembedding"
3640
"github.com/symonk/learning-golang/structs"
@@ -42,9 +46,6 @@ import (
4246
"github.com/symonk/learning-golang/variables"
4347
"github.com/symonk/learning-golang/waitgroups"
4448
"github.com/symonk/learning-golang/workerpools"
45-
"github.com/symonk/learning-golang/mutexes"
46-
"github.com/symonk/learning-golang/statefulgoroutines"
47-
"github.com/symonk/learning-golang/sorting"
4849
)
4950

5051
func main() {
@@ -114,5 +115,6 @@ func buildMap() map[string]func() {
114115
fnMap["mutexes"] = mutexes.Run
115116
fnMap["statefulgoroutines"] = statefulgoroutines.Run
116117
fnMap["sorting"] = sorting.Run
118+
fnMap["sortingbyfunctions"] = sortingbyfunctions.Run
117119
return fnMap
118120
}

sorting/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Foo struct {
2828

2929
// Sorting a user defined; custom type
3030
func sortingCustomType() {
31-
foos := []Foo{Foo{x: 3}, Foo{x: 2}, Foo{x: 100}, Foo{x: 1}}
31+
foos := []Foo{{x: 3}, {x: 2}, {x: 100}, {x: 1}}
3232
sort.Slice(foos[:], func(i, j int) bool {
3333
return foos[i].x < foos[j].x
3434
})

sortingbyfunctions/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sortingbyfunctions
2+
3+
import (
4+
"cmp"
5+
"fmt"
6+
"slices"
7+
)
8+
9+
// Often you want to sort by something other than the
10+
// natural order
11+
func Run() {
12+
13+
ints := []int{2, 3, 12, 13, 22, 23}
14+
sortingIntegersByOddEven(ints)
15+
fmt.Println(ints)
16+
// [2, 12, 22, 3, 13, 23]
17+
}
18+
19+
// Sort our slice in-place by a custom function.
20+
func sortingIntegersByOddEven(ints []int) {
21+
compFn := func(a, b int) int {
22+
return cmp.Compare[int](a%2, b%2)
23+
}
24+
slices.SortFunc(ints, compFn)
25+
}

0 commit comments

Comments
 (0)