Answer the question
In order to leave comments, you need to log in
How to sort in Go?
Suppose I have a slice of bytes. How to sort it in Go?
arr := []byte{4, 3, 2, 1, 6, 3, 77, 8,3}
sort.Slice(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
fmt.Println(arr)
Answer the question
In order to leave comments, you need to log in
If you need a function, you can do it like this
func SortBytes(arr []byte) {
sort.Slice(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
}
// потом использовать вот так
arr := []byte{4, 3, 2, 1, 6, 3, 77, 8,3}
sortBytes(arr)
fmt.Println(arr)
sort.Slice(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
fmt.Println(arr)
sorter := func(i, j int) bool {
return arr[i] < arr[j]
}
sort.Slice(arr, sorter)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question