Answer the question
In order to leave comments, you need to log in
How to sort slice of ints in GO?
Sorting a slice of an int using the sort package. But the output is an unsorted array. Where is the mistake?
package main
import (
"fmt"
"sort"
)
func main() {
var arr = []int{1, 3, 2}
sort.SliceStable(arr, func(i, j int) bool {
return i > j
})
for _, val := range arr {
fmt.Println(val)
}
}
Answer the question
In order to leave comments, you need to log in
Sort by index, but you need by value
package main
import (
"fmt"
"sort"
)
func main() {
var arr = []int{1, 3, 2}
sort.SliceStable(arr, func(i, j int) bool {
return arr[i] > arr[j] // здесь поправил
})
for _, val := range arr {
fmt.Println(val)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question