Skip to content

Commit

Permalink
Sort usergroups by their oktalabels if available (#35729)
Browse files Browse the repository at this point in the history
  • Loading branch information
avatus committed Dec 19, 2023
1 parent 1f9efb8 commit 63b7d63
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions api/types/usergroup.go
Expand Up @@ -19,6 +19,7 @@ package types
import (
"fmt"
"sort"
"strings"

"github.com/gravitational/trace"

Expand Down Expand Up @@ -112,11 +113,37 @@ func (g UserGroups) SortByCustom(sortBy SortBy) error {
switch sortBy.Field {
case ResourceMetadataName:
sort.SliceStable(g, func(i, j int) bool {
return stringCompare(g[i].GetName(), g[j].GetName(), isDesc)
groupA := g[i]
groupB := g[j]

groupAName := FriendlyName(groupA)
groupBName := FriendlyName(groupB)

if groupAName == "" {
groupAName = groupA.GetName()
}
if groupBName == "" {
groupBName = groupB.GetName()
}

return stringCompare(strings.ToLower(groupAName), strings.ToLower(groupBName), isDesc)
})
case ResourceSpecDescription:
sort.SliceStable(g, func(i, j int) bool {
return stringCompare(g[i].GetMetadata().Description, g[j].GetMetadata().Description, isDesc)
groupA := g[i]
groupB := g[j]

groupADescription := groupA.GetMetadata().Description
groupBDescription := groupB.GetMetadata().Description

if oktaDescription, ok := groupA.GetLabel(OktaGroupDescriptionLabel); ok {
groupADescription = oktaDescription
}
if oktaDescription, ok := groupB.GetLabel(OktaGroupDescriptionLabel); ok {
groupBDescription = oktaDescription
}

return stringCompare(strings.ToLower(groupADescription), strings.ToLower(groupBDescription), isDesc)
})

default:
Expand Down

0 comments on commit 63b7d63

Please sign in to comment.