Function signature
func Sor(fn interface{})Arguments
| Name | Type | Description |
|---|---|---|
| item | func | This function must receive two arguments with the same tup that the elements in the stream and must return an int (-1,0,1) taht will determine which of the two items must be ordered before the other |
Output
NoneErrors
| Type | Message |
|---|---|
| err.items-nil | A nil stream can not be sorted |
| err.invalid-argument | The filter operation requires a function as argument |
| err.invalid-argument | The provided function must retrieve 2 arguments |
| err.invalid-argument | The provided function must return 1 value |
| err.invalid-argument | The type of the both arguments must be %s |
| err.invalid-argument | The type of the output in the provided function must be int |
Examples
package main
import (
"fmt"
"github.com/wesovilabs/koazee"
)
var numbers = []int{1, 3, 5, 7, 9}
func main() {
fmt.Println(koazee.Stream().
With(numbers).
Sort(func(val1, val2 int) int {
if val1 > val2 {
return 1
} else if val1 < val2 {
return -1
}
return 0
}).Out().Val())
}package main
import (
"fmt"
"github.com/wesovilabs/koazee"
)
type genre int
const (
female genre = iota
male
)
type primate struct {
name string
age int
family string
genre genre
}
func newPrimate(name string, age int, family string, genre genre) *primate {
return &primate{
name: name,
age: age,
family: family,
genre: genre,
}
}
var primates = []*primate{
newPrimate("John", 15, "Capuchin", male),
newPrimate("Laura", 12, "Squirrel monkey", female),
newPrimate("Benjamin", 23, "Spider monkey", male),
newPrimate("George", 19, "Golden Lion Tamarin", male),
newPrimate("Jane", 33, "Orangutan", female),
newPrimate("Sarah", 7, "Gibbon", female),
}
func main() {
primates := koazee.StreamOf(primates).
Sort(func(primate1, primate2 *primate) int {
if primate1.age > primate2.age {
return -1
} else if primate1.age < primate2.age {
return 1
}
return 0
}).Out().Val().([]*primate)
for _, primate := range primates {
fmt.Println(primate.name)
}
}