Sort strings by length and then abc order
examples/sort-two-conditions/sort_by_two_conditions.go
package main import ( "fmt" "sort" ) func main() { animals := []string{"snail", "dog", "cow", "elephant", "chicken", "mouse"} fmt.Println(animals) sort.Slice(animals, func(i, j int) bool { if len(animals[i]) != len(animals[j]) { return len(animals[i]) < len(animals[j]) } return animals[i] < animals[j] }) fmt.Println(animals) }
[snail dog cow elephant chicken mouse] [cow dog mouse snail chicken elephant]