grafana/pkg/services/search/sorting.go
idafurjes 3bda112c5f
Chore: Move search model from models package to search service (#62215)
* Chore: Move search model from models package to search service

* Remove unused imports

* Cleanup after merge
2023-01-30 15:17:53 +01:00

47 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package search
import (
"sort"
"github.com/grafana/grafana/pkg/services/search/model"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
)
var (
SortAlphaAsc = model.SortOption{
Name: "alpha-asc",
DisplayName: "Alphabetically (AZ)",
Description: "Sort results in an alphabetically ascending order",
Index: 0,
Filter: []model.SortOptionFilter{
searchstore.TitleSorter{},
},
}
SortAlphaDesc = model.SortOption{
Name: "alpha-desc",
DisplayName: "Alphabetically (ZA)",
Description: "Sort results in an alphabetically descending order",
Index: 0,
Filter: []model.SortOptionFilter{
searchstore.TitleSorter{Descending: true},
},
}
)
// RegisterSortOption allows for hooking in more search options from
// other services.
func (s *SearchService) RegisterSortOption(option model.SortOption) {
s.sortOptions[option.Name] = option
}
func (s *SearchService) SortOptions() []model.SortOption {
opts := make([]model.SortOption, 0, len(s.sortOptions))
for _, o := range s.sortOptions {
opts = append(opts, o)
}
sort.Slice(opts, func(i, j int) bool {
return opts[i].Index < opts[j].Index || (opts[i].Index == opts[j].Index && opts[i].Name < opts[j].Name)
})
return opts
}