Simplify sort code a bit
Switch to sort.Slice() instead of sort.Sort(), which requires less boilerplate.
This commit is contained in:
committed by
Ralf Haferkamp
parent
1ed52fb57f
commit
4717248959
@@ -486,24 +486,32 @@ func isValidEmail(e string) bool {
|
||||
}
|
||||
|
||||
func sortUsers(req *godata.GoDataRequest, users []*libregraph.User) ([]*libregraph.User, error) {
|
||||
var sorter sort.Interface
|
||||
if req.Query.OrderBy == nil || len(req.Query.OrderBy.OrderByItems) != 1 {
|
||||
return users, nil
|
||||
}
|
||||
var less func(i, j int) bool
|
||||
|
||||
switch req.Query.OrderBy.OrderByItems[0].Field.Value {
|
||||
case displayNameAttr:
|
||||
sorter = usersByDisplayName{users}
|
||||
less = func(i, j int) bool {
|
||||
return strings.ToLower(users[i].GetDisplayName()) < strings.ToLower(users[j].GetDisplayName())
|
||||
}
|
||||
case "mail":
|
||||
sorter = usersByMail{users}
|
||||
less = func(i, j int) bool {
|
||||
return strings.ToLower(users[i].GetMail()) < strings.ToLower(users[j].GetMail())
|
||||
}
|
||||
case "onPremisesSamAccountName":
|
||||
sorter = usersByOnPremisesSamAccountName{users}
|
||||
less = func(i, j int) bool {
|
||||
return strings.ToLower(users[i].GetOnPremisesSamAccountName()) < strings.ToLower(users[j].GetOnPremisesSamAccountName())
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("we do not support <%s> as a order parameter", req.Query.OrderBy.OrderByItems[0].Field.Value)
|
||||
}
|
||||
|
||||
if req.Query.OrderBy.OrderByItems[0].Order == _sortDescending {
|
||||
sorter = sort.Reverse(sorter)
|
||||
sort.Slice(users, reverse(less))
|
||||
} else {
|
||||
sort.Slice(users, less)
|
||||
}
|
||||
sort.Sort(sorter)
|
||||
return users, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user