github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/sliceexamples/clone_sort_By_test.go (about) 1 package sliceexamples 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/m4gshm/gollections/slice/clone/sort" 9 ) 10 11 func Test_SortBy(t *testing.T) { 12 13 var byName = sort.By(users, User.Name) 14 //[{Alice 35 []} {Bob 26 []} {Chris 41 []} {Tom 18 []}] 15 16 var byAgeReverse = sort.DescBy(users, User.Age) 17 //[{Chris 41 []} {Alice 35 []} {Bob 26 []} {Tom 18 []}] 18 19 assert.Equal(t, []User{ 20 {name: "Alice", age: 35}, 21 {name: "Bob", age: 26}, 22 {name: "Chris", age: 41}, 23 {name: "Tom", age: 18}, 24 }, byName) 25 assert.Equal(t, []User{ 26 {name: "Chris", age: 41}, 27 {name: "Alice", age: 35}, 28 {name: "Bob", age: 26}, 29 {name: "Tom", age: 18}, 30 }, byAgeReverse) 31 }