Skip to content

Commit 412e660

Browse files
wilfredo-malazarteWilfredo MalazarteLFDanLu
authored
Update documentation to use useCollator hook (#2656)
* update documentation to use collator hook * add collator options and missing import * remove whitespace and extra import Co-authored-by: Wilfredo Malazarte <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent 7e04538 commit 412e660

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

packages/@react-spectrum/table/docs/TableView.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ This example performs client side sorting by passing a `sort` function to the [u
497497
See the docs for more information on how to perform server side sorting.
498498

499499
```tsx example
500+
import { useCollator } from '@react-aria/i18n';
501+
500502
function AsyncSortTable() {
503+
let collator = useCollator({numeric: true});
504+
501505
let list = useAsyncList({
502506
async load({signal}) {
503507
let res = await fetch(`https://swapi.dev/api/people/?search`, {signal});
@@ -511,7 +515,7 @@ function AsyncSortTable() {
511515
items: items.sort((a, b) => {
512516
let first = a[sortDescriptor.column];
513517
let second = b[sortDescriptor.column];
514-
let cmp = (parseInt(first) || first) < (parseInt(second) || second) ? -1 : 1;
518+
let cmp = collator.compare(first, second);
515519
if (sortDescriptor.direction === 'descending') {
516520
cmp *= -1;
517521
}

packages/@react-stately/data/docs/useAsyncList.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ This example shows how to implement client side sorting by passing a `sort` func
129129
items array.
130130

131131
```tsx
132+
let collator = useCollator();
133+
132134
let list = useAsyncList({
133135
async load({signal}) {
134136
// Same load function as before
@@ -137,7 +139,7 @@ let list = useAsyncList({
137139
return {
138140
items: items.sort((a, b) => {
139141
// Compare the items by the sorted column
140-
let cmp = a[sortDescriptor.column] < b[sortDescriptor.column] ? -1 : 1;
142+
let cmp = collator.compare(a[sortDescriptor.column], b[sortDescriptor.column]);
141143

142144
// Flip the direction if descending order is specified.
143145
if (sortDescriptor.direction === 'descending') {

0 commit comments

Comments
 (0)