@@ -5,48 +5,48 @@ export type SortConfig = {
55 direction : 'ascending' | 'descending' ;
66} ;
77
8- export const useSorting = < T extends Record < string , any > > ( data : T [ ] ) => {
9- const [ sortConfig , setSortConfig ] = useState < SortConfig > ( {
10- key : null ,
11- direction : 'ascending'
12- } ) ;
8+ export const useSorting = < T extends Record < string , any > > ( data : T [ ] ) => {
9+ const [ sortConfig , setSortConfig ] = useState < SortConfig > ( {
10+ key : null ,
11+ direction : 'ascending'
12+ } ) ;
1313
14- const handleSort = ( key : string ) => {
14+ const handleSort = ( key : string ) => {
1515 let direction = 'ascending' ;
16- if ( sortConfig . key === key && sortConfig . direction === 'ascending' ) {
16+ if ( sortConfig . key === key && sortConfig . direction === 'ascending' ) {
1717 direction = 'descending' ;
1818 }
19- setSortConfig ( { key, direction : null } ) ;
19+ setSortConfig ( { key, direction : "descending" } ) ;
2020 } ;
2121
22- const sortedData = useMemo ( ( ) => {
23- if ( ! sortConfig . key ) return data ;
24-
25- return [ ...data ] . sort ( ( a , b ) => {
26- const valueA = a [ sortConfig . key ! ] ;
27- const valueB = b [ sortConfig . key ! ] ;
28-
29- const isNumericA = ! isNaN ( parseFloat ( valueA ) ) ;
30- const isNumericB = ! isNaN ( parseFloat ( valueB ) ) ;
31-
32- if ( isNumericA && isNumericB ) {
33- const numericA = parseFloat ( valueA ) ;
34- const numericB = parseFloat ( valueB ) ;
35- return sortConfig . direction === 'ascending'
36- ? numericA - numericB
22+ const sortedData = useMemo ( ( ) => {
23+ if ( ! sortConfig . key ) return data ;
24+
25+ return [ ...data ] . sort ( ( a , b ) => {
26+ const valueA = a [ sortConfig . key ! ] ;
27+ const valueB = b [ sortConfig . key ! ] ;
28+
29+ const isNumericA = ! isNaN ( parseFloat ( valueA ) ) ;
30+ const isNumericB = ! isNaN ( parseFloat ( valueB ) ) ;
31+
32+ if ( isNumericA && isNumericB ) {
33+ const numericA = parseFloat ( valueA ) ;
34+ const numericB = parseFloat ( valueB ) ;
35+ return sortConfig . direction === 'ascending'
36+ ? numericA - numericB
3737 : numericB - numericA ;
3838 }
39-
40- if ( isNumericA ) return sortConfig . direction === 'ascending' ? - 1 : 1 ;
41- if ( isNumericB ) return sortConfig . direction === 'descending' ? 1 : - 1 ;
42-
43- const result = String ( valueA || '' ) . localeCompare ( String ( valueB || '' ) ) ;
39+
40+ if ( isNumericA ) return sortConfig . direction === 'ascending' ? - 1 : 1 ;
41+ if ( isNumericB ) return sortConfig . direction === 'descending' ? 1 : - 1 ;
42+
43+ const result = String ( valueA || '' ) . localeCompare ( String ( valueB || '' ) ) ;
4444 return sortConfig . direction === 'ascending' ? result : - result ;
45- } ) ;
46- } , [ data , sortConfig ] ) ;
45+ } ) ;
46+ } , [ data , sortConfig ] ) ;
4747
48- const getSortIcon = ( key : string ) => {
49- if ( sortConfig . key !== key ) return null ;
48+ const getSortIcon = ( key : string ) => {
49+ if ( sortConfig . key !== key ) return null ;
5050 return sortConfig . direction === 'ascending' ? '▲' : '▼' ;
5151 } ;
5252
0 commit comments