Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Sort table when clicking the table head

I'm trying to make a sortable user table that can be sorted by all the table heads. I have been able to get it to sort ascending and descending, but only by the name column. I'm new to reactjs and have not been able to implement it in my code.

here is the table component:

import { formatDate } from "../../utils/formatDate";
import "./table.css";

function Table(props) {
  const { headerData, bodyData, removeItem, sortData, ordIcon} = props;

  
  return (
    <div className="user-data">
      <table className="user-table">
        <thead>
          <tr className="data-th">
            {headerData.map((headerTable) => (
              <th onClick={sortData} id={"header-"+headerTable.toLowerCase()}>
                {headerTable} <img src={ordIcon} id="arrow-img"></img>
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {bodyData.map((item) => (
            <tr className="data-tr">
              <td>{item.name}</td>
              <td>{item.email}</td>
              <td>{item.occupation}</td>
              <td>{formatDate(item.birthday)}</td>               
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}

export default Table;

and here is the sorting code in the Home:

const sortData = () => {
    
    if (order === "" || "asc") {
      let sortUserName = [...newUserArr].sort((a, b) =>
        a.name.localeCompare(b.name)
      );
      setOrder("dsc");
      setNewUserArr(sortUserName);
      setOrdIcon(arrowDown)
    } if (order === "dsc") {
      let sortUserName = [...newUserArr].sort((a, b) =>
        b.name.localeCompare(a.name)
      );
      setOrder("asc");
      setNewUserArr(sortUserName);
      setOrdIcon(arrowUp)
    }
  };

Thank you

edit: headerData={headerUser}

const headerUser = ["Name", "Email", "Occupation", "Birthday"];

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here's one generic approach,

In Home component, add a variable storing the key and type of every column

    const columnMap = {
        Name: {key: 'name', type: 'string'},
        Email: {key: 'email', type: 'string'},
        Occupation: {key: 'occupation', type: 'string'},
        Birthday: {key: 'birthday', type: 'date'}
    };

And then in the Table component while rendering the column headers, change the onClick so that it sends the columnHeader to sortData function

{headerData.map((headerTable) => (
  <th onClick={() => sortData(headerTable)} id={"header-"+headerTable.toLowerCase()}>
     {headerTable} <img src={ordIcon} id="arrow-img"></img>
  </th>
))}

And finally in the sortData function,

const sortData = (columnHeader) => {
    const {key, type} = columnMap[columnHeader];

    const sorted = [...newUserArr].sort((a, b) => {
        let sortValue = 0;

        if (type === 'date') {
            sortValue = new Date(a[key]) - new Date(b[key]);
        } else {
            sortValue = a[key].localeCompare(b[key]);
        }

        return order === 'dsc' ? -(sortValue) : sortValue;
    });

    setNewUserArr(sorted);

    if (order === '' || 'asc') {
        setOrder('dsc');
        setOrdIcon(arrowDown);
    } else {
        setOrder('asc');
        setOrdIcon(arrowUp);
    }    
};

We first find the key using which we need to sort newUserArr, we also need type here because for dates we cannot use localeCompare. Then we sort the array, if order is dsc then we reverse the sort.

EDIT - For sort icons, we could keep track of the current sort state of each column and then use it in Table component

In Home component

const [sortStatus, setSortStatus] = useState({});

And change sortData to

const sortData = (columnHeader) => {
    const {key, type} = columnMap[columnHeader];

    const sorted = [...newUserArr].sort((a, b) => {
        let sortValue = 0;

        if (type === 'date') {
            sortValue = new Date(a[key]) - new Date(b[key]);
        } else {
            sortValue = a[key].localeCompare(b[key]);
        }

        return order === 'dsc' ? -(sortValue) : sortValue;
    });

    setNewUserArr(sorted);

    setSortStatus((prev) => ({
        ...prev,
        [columnHeader]: order === 'dsc' ? 'asc' : 'dsc'
    }))
};

In Table component,

const getSortIcon = (columnHeader) => {
   if (!sortStatus[columnHeader]) {
      return "↕︎";
   }
   
   return sortStatus[columnHeader] === 'dsc' ? "↓" : "↑"; 
};

{headerData.map((headerTable) => (
    <th onClick={sortData} id={"header-"+headerTable.toLowerCase()}>
    {headerTable}
    {getSortIcon(headerTable)}
    </th>
))}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda