Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/guide/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ By default, there are 6 built-in sorting functions to choose from:

- `alphanumeric` - Sorts by mixed alphanumeric values without case-sensitivity. Slower, but more accurate if your strings contain numbers that need to be naturally sorted.
- `alphanumericCaseSensitive` - Sorts by mixed alphanumeric values with case-sensitivity. Slower, but more accurate if your strings contain numbers that need to be naturally sorted.
- `alphanumericIgnoreDiacritics` - Sorts by mixed alphanumeric values without case-sensitivity and ignoring diacritics/accent marks. Slower, but more accurate if your strings contain numbers that need to be naturally sorted.
- `alphanumericIgnoreDiacriticsCaseSensitive` - Sorts by mixed alphanumeric values with case-sensitivity and ignoring diacritics/accent marks. Slower, but more accurate if your strings contain numbers that need to be naturally sorted.
- `text` - Sorts by text/string values without case-sensitivity. Faster, but less accurate if your strings contain numbers that need to be naturally sorted.
- `textCaseSensitive` - Sorts by text/string values with case-sensitivity. Faster, but less accurate if your strings contain numbers that need to be naturally sorted.
- `textIgnoreDiacritics` - Sorts by text/string values without case-sensitivity and ignoring diacritics/accent marks. Faster, but less accurate if your strings contain numbers that need to be naturally sorted.
- `textIgnoreDiacriticsCaseSensitive` - Sorts by text/string values with case-sensitivity and ignoring diacritics/accent marks. Faster, but less accurate if your strings contain numbers that need to be naturally sorted.
- `datetime` - Sorts by time, use this if your values are `Date` objects.
- `basic` - Sorts using a basic/standard `a > b ? 1 : a < b ? -1 : 0` comparison. This is the fastest sorting function, but may not be the most accurate.

Expand Down
69 changes: 69 additions & 0 deletions packages/table-core/src/fns/sortFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ export const sortFn_alphanumericCaseSensitive: SortFn<any, any> = <
)
}

export const sortFn_alphanumericIgnoreDiacritics: SortFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
>(
rowA: Row<any, any>,
rowB: Row<any, any>,
columnId: string,
) => {
return compareAlphanumeric(
normalizeText(toString(rowA.getValue(columnId)).toLowerCase()),
normalizeText(toString(rowB.getValue(columnId)).toLowerCase()),
)
}

export const sortFn_alphanumericIgnoreDiacriticsCaseSensitive: SortFn<
any,
any
> = <TFeatures extends TableFeatures, TData extends RowData>(
rowA: Row<any, any>,
rowB: Row<any, any>,
columnId: string,
) => {
return compareAlphanumeric(
normalizeText(toString(rowA.getValue(columnId))),
normalizeText(toString(rowB.getValue(columnId))),
)
}

// The text filter is more basic (less numeric support)
// but is much faster
export const sortFn_text: SortFn<any, any> = <
Expand Down Expand Up @@ -65,6 +93,38 @@ export const sortFn_textCaseSensitive: SortFn<any, any> = <
)
}

// The text filter is more basic (less numeric support)
// but is much faster
export const sortFn_textIgnoreDiacritics: SortFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
>(
rowA: Row<any, any>,
rowB: Row<any, any>,
columnId: string,
) => {
return compareBasic(
normalizeText(toString(rowA.getValue(columnId)).toLowerCase()),
normalizeText(toString(rowB.getValue(columnId)).toLowerCase()),
)
}

// The text filter is more basic (less numeric support)
// but is much faster
export const sortFn_textIgnoreDiacriticsCaseSensitive: SortFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
>(
rowA: Row<any, any>,
rowB: Row<any, any>,
columnId: string,
) => {
return compareBasic(
normalizeText(toString(rowA.getValue(columnId))),
normalizeText(toString(rowB.getValue(columnId))),
)
}

export const sortFn_datetime: SortFn<any, any> = <
TFeatures extends TableFeatures,
TData extends RowData,
Expand Down Expand Up @@ -112,6 +172,10 @@ function toString(a: any) {
return ''
}

function normalizeText(str: string): string {
return str.normalize('NFD').replace(/\p{Diacritic}/gu, '')
}

// Mixed sorting is slow, but very inclusive of many edge cases.
// It handles numbers, mixed alphanumeric combinations, and even
// null, undefined, and Infinity
Expand Down Expand Up @@ -164,10 +228,15 @@ function compareAlphanumeric(aStr: string, bStr: string) {
export const sortFns = {
alphanumeric: sortFn_alphanumeric,
alphanumericCaseSensitive: sortFn_alphanumericCaseSensitive,
alphanumericIgnoreDiacritics: sortFn_alphanumericIgnoreDiacritics,
alphanumericIgnoreDiacriticsCaseSensitive:
sortFn_alphanumericIgnoreDiacriticsCaseSensitive,
basic: sortFn_basic,
datetime: sortFn_datetime,
text: sortFn_text,
textCaseSensitive: sortFn_textCaseSensitive,
textIgnoreDiacritics: sortFn_textIgnoreDiacritics,
textIgnoreDiacriticsCaseSensitive: sortFn_textIgnoreDiacriticsCaseSensitive,
}

export type BuiltInSortFn = keyof typeof sortFns
Loading