diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 3b16bfeed80..b4f78a5a6f0 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -3,6 +3,7 @@ package sqlstore import ( "context" "fmt" + "sort" "strconv" "strings" "time" @@ -311,6 +312,27 @@ func GetUserProfile(query *models.GetUserProfileQuery) error { return err } +type byOrgName []*models.UserOrgDTO + +// Len returns the length of an array of organisations. +func (o byOrgName) Len() int { + return len(o) +} + +// Swap swaps two indices of an array of organizations. +func (o byOrgName) Swap(i, j int) { + o[i], o[j] = o[j], o[i] +} + +// Less returns whether element i of an array of organizations is less than element j. +func (o byOrgName) Less(i, j int) bool { + if strings.ToLower(o[i].Name) < strings.ToLower(o[j].Name) { + return true + } + + return o[i].Name < o[j].Name +} + func GetUserOrgList(query *models.GetUserOrgListQuery) error { query.Result = make([]*models.UserOrgDTO, 0) sess := x.Table("org_user") @@ -319,6 +341,7 @@ func GetUserOrgList(query *models.GetUserOrgListQuery) error { sess.Cols("org.name", "org_user.role", "org_user.org_id") sess.OrderBy("org.name") err := sess.Find(&query.Result) + sort.Sort(byOrgName(query.Result)) return err } diff --git a/public/app/core/components/OrgSwitcher.tsx b/public/app/core/components/OrgSwitcher.tsx index 39b13958d7a..1954b3aa399 100644 --- a/public/app/core/components/OrgSwitcher.tsx +++ b/public/app/core/components/OrgSwitcher.tsx @@ -26,9 +26,7 @@ export class OrgSwitcher extends React.PureComponent { getUserOrgs = async () => { const orgs: UserOrgDTO[] = await getBackendSrv().get('/api/user/orgs'); - this.setState({ - orgs: orgs.sort((a, b) => a.orgId - b.orgId), - }); + this.setState({ orgs }); }; setCurrentOrg = async (org: UserOrgDTO) => {