0
export const COLUMNS = [
    {
        Header : 'Total Cases',
        accessor : 'totalcases',
    },
    {
        Header : 'Active Cases',
        accessor : 'activecases',
    },
    {
        Header : 'Total Recovery',
        accessor : 'recovery',
    },
    {
        Header : 'Total Death',
        accessor : 'deaths',
    },
    {
        Header : 'New Cases',
        accessor : 'newcases',
    },
    
] 

function Table({countryData}) {

    const columns = useMemo(()=> COLUMNS, []);
    const data = useMemo(()=> countryData, []);

    const {
        ....
        setGlobalFilter,
    } =  useTable({
        columns,
        data
    }, useGlobalFilter, useSortBy);

    const{globalFilter} = state;

    return (
        <>
        <GlobalFilter filter={globalFilter} setFilter={setGlobalFilter}/>
        <table {...getTableProps()}>
            <thead>
                
                {headerGroups.map(headerGroup => (
                     <tr {...headerGroup.getHeaderGroupProps()}>
                        {headerGroup.headers.map(column => (
                            <th {...column.getHeaderProps(column.getSortByToggleProps())}>{column.render('Header')}</th>
                ))}
          </tr>
        ))}
            </thead>
            <tbody {...getTableBodyProps()}>
                {rows.map((row, i) => {
                    prepareRow(row)
                    return (
                        <tr {...row.getRowProps()}>
                                {row.cells.map(cell => {
                                    return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
                            })}
                        </tr>
                    )
                })}
            </tbody>
        </table>
        </>
    )
}

export default Table

here I want to sort the entire table by default on the basis of new cases column and want toggle sort only on active cases and recovery. as Im new to react i dont know how to provide custom sort..............................................................................................................................................................................................................................................................

ASD
  • 1
  • 1
  • 1
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – K.K Designs Jan 12 '22 at 17:38

1 Answers1

0

you can use : columns.sort((a, b) => (a.accessor > b.accessor ) ? 1 : -1) More informision at this link: https://flaviocopes.com/how-to-sort-array-of-objects-by-property-javascript/

AkifObaidi
  • 17
  • 1
  • 9