Medium
You're building a data grid component that needs to handle sorting, filtering, and pagination efficiently. What's missing in this implementation to make sorting work correctly?
1function DataGrid({ data, columns }) {2 const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' });3 const [currentPage, setCurrentPage] = useState(1);4 const [itemsPerPage] = useState(10);56 // Missing implementation7 _______________89 const sortedData = useMemo(() => {10 if (!sortConfig.key) return data;1112 return [...data].sort((a, b) => {13 const aVal = a[sortConfig.key];14 const bVal = b[sortConfig.key];1516 if (sortConfig.direction === 'asc') {17 return aVal > bVal ? 1 : -1;18 }19 return aVal < bVal ? 1 : -1;20 });21 }, [data, sortConfig]);2223 const paginatedData = sortedData.slice(24 (currentPage - 1) * itemsPerPage,25 currentPage * itemsPerPage26 );2728 return (29 <div className="data-grid">30 <table>31 <thead>32 <tr>33 {columns.map(column => (34 <th35 key={column.key}36 onClick={() => requestSort(column.key)}37 className={sortConfig.key === column.key ? sortConfig.direction : ''}38 >39 {column.label}40 </th>41 ))}42 </tr>43 </thead>44 <tbody>45 {paginatedData.map(row => (46 <tr key={row.id}>47 {columns.map(column => (48 <td key={column.key}>{row[column.key]}</td>49 ))}50 </tr>51 ))}52 </tbody>53 </table>54 </div>55 );56}