@@ -6,7 +6,13 @@ import {CompactSelect} from 'sentry/components/compactSelect';
66import { Tooltip } from 'sentry/components/tooltip' ;
77import { t } from 'sentry/locale' ;
88import type { Sort } from 'sentry/utils/discover/fields' ;
9- import { parseFunction , prettifyParsedFunction } from 'sentry/utils/discover/fields' ;
9+ import {
10+ parseFunction ,
11+ prettifyParsedFunction ,
12+ prettifyTagKey ,
13+ TYPED_TAG_KEY_RE ,
14+ } from 'sentry/utils/discover/fields' ;
15+ import { FieldKind } from 'sentry/utils/fields' ;
1016import { TypeBadge } from 'sentry/views/explore/components/typeBadge' ;
1117import { useSpanTags } from 'sentry/views/explore/contexts/spanTagsContext' ;
1218import { useResultMode } from 'sentry/views/explore/hooks/useResultsMode' ;
@@ -32,36 +38,59 @@ export function ToolbarSortBy({fields, setSorts, sorts}: ToolbarSortByProps) {
3238 const stringTags = useSpanTags ( 'string' ) ;
3339
3440 const fieldOptions : SelectOption < Field > [ ] = useMemo ( ( ) => {
35- return fields . map ( field => {
36- const tag = stringTags [ field ] ?? numberTags [ field ] ?? null ;
37- if ( tag ) {
41+ const options = [
42+ ...new Set ( fields ) . keys ( ) . map ( field => {
43+ const tag = stringTags [ field ] ?? numberTags [ field ] ?? null ;
44+ if ( tag ) {
45+ return {
46+ label : tag . name ,
47+ value : field ,
48+ textValue : tag . name ,
49+ trailingItems : < TypeBadge kind = { tag ?. kind } /> ,
50+ } ;
51+ }
52+
53+ const func = parseFunction ( field ) ;
54+ if ( func ) {
55+ const formatted = prettifyParsedFunction ( func ) ;
56+ return {
57+ label : formatted ,
58+ value : field ,
59+ textValue : formatted ,
60+ trailingItems : < TypeBadge func = { func } /> ,
61+ } ;
62+ }
63+
64+ const result = field . match ( TYPED_TAG_KEY_RE ) ;
65+ const kind =
66+ result ?. [ 2 ] === 'string'
67+ ? FieldKind . TAG
68+ : result ?. [ 2 ] === 'number'
69+ ? FieldKind . MEASUREMENT
70+ : undefined ;
71+
3872 return {
39- label : tag . name ,
73+ label : prettifyTagKey ( field ) ,
4074 value : field ,
41- textValue : tag . name ,
42- trailingItems : < TypeBadge tag = { tag } /> ,
75+ textValue : field ,
76+ trailingItems : < TypeBadge kind = { kind } /> ,
4377 } ;
78+ } ) ,
79+ ] ;
80+
81+ options . sort ( ( a , b ) => {
82+ if ( a . label < b . label ) {
83+ return - 1 ;
4484 }
4585
46- const func = parseFunction ( field ) ;
47- if ( func ) {
48- const formatted = prettifyParsedFunction ( func ) ;
49- return {
50- label : formatted ,
51- value : field ,
52- textValue : formatted ,
53- trailingItems : < TypeBadge func = { func } /> ,
54- } ;
86+ if ( a . label > b . label ) {
87+ return 1 ;
5588 }
5689
57- // not a tag, maybe it's an aggregate
58- return {
59- label : field ,
60- value : field ,
61- textValue : field ,
62- trailingItems : < TypeBadge tag = { tag } /> ,
63- } ;
90+ return 0 ;
6491 } ) ;
92+
93+ return options ;
6594 } , [ fields , numberTags , stringTags ] ) ;
6695
6796 const setSortField = useCallback (
0 commit comments