package CommonUtil import ( "reflect" "sort" ) type Items struct { data interface{} field string } func (items *Items) Len() int { if reflect.ValueOf(items.data).Kind() != reflect.Slice { return -1 } return reflect.ValueOf(items.data).Len() } func (items *Items) Less(i, j int) bool { a := reflect.ValueOf(items.data).Index(i) b := reflect.ValueOf(items.data).Index(j) if a.Kind() == reflect.Ptr { a = a.Elem() } if b.Kind() == reflect.Ptr { b = b.Elem() } va := a.FieldByName(items.field).Int() vb := b.FieldByName(items.field).Int() return va < vb } func (items *Items) Swap(i, j int) { reflect.Swapper(items.data)(i, j) } func SortItems(i interface{}, str string, sortType string) { if reflect.ValueOf(i).Kind() != reflect.Slice { return } a := &Items{ data: i, field: str, } if sortType == "desc" { sort.Sort(sort.Reverse(a)) } else { sort.Sort(a) } }