0

I have the next code:

import React, { useState } from "react";
import "./styles.css";

export default function App() {
  const [state, setState] = useState([
    { name: "Edward", value: 21 },
    { name: "Sharpe", value: 37 },
    { name: "And", value: 45 },
    { name: "The", value: -12 },
    { name: "Magnetic" },
    { name: "Zeros", value: 37 }
  ]);

  const sorting = () => {
    const a = state.sort(function (a, b) {
      if (a.name > b.name) {
        return 1;
      }
      if (a.name < b.name) {
        return -1;
      }
      return 0;
    });
    setState(a);
  };

  return (
    <div className="App">
      <table>
        <tr>
          <th>Firstname</th>
          <th>data</th>
        </tr>
        {state.map((i) => {
          return (
            <tr key={i.name}>
              <td>{i.name}</td>
              <td>{i.value}</td>
            </tr>
          );
        })}
      </table>
      <button onClick={sorting}>sort</button>
    </div>
  );
}

Clicking on the button i want to trigger the next function:

 const sorting = () => {
    const a = state.sort(function (a, b) {
      if (a.name > b.name) {
        return 1;
      }
      if (a.name < b.name) {
        return -1;
      }
      return 0;
    });
    setState(a);
  };

Which should sort the data from table, but it does not happens.
Question: Why my function does not work? ow to solve the issue that i described above? Note: when i will click on the button the data from table should change the order.

1
  • do you want to sort the list based on the alphabetical order of name or on the basis of value? Commented Dec 5, 2020 at 12:39

1 Answer 1

3

Before sorting convert the name to either uppercase or lowercase. After that, you can easily sort it with the help of localeCompare():

let data = [
  { name: "Edward", value: 21 },
  { name: "Sharpe", value: 37 },
  { name: "And", value: 45 },
  { name: "The", value: -12 },
  { name: "Magnetic" },
  { name: "Zeros", value: 37 },
];

data.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));

console.log(data);

Full react app:

enter image description here

import React, { useState, useEffect } from "react";

export default function App() {
  const [state, setState] = useState([
    { name: "Edward", value: 21 },
    { name: "Sharpe", value: 37 },
    { name: "And", value: 45 },
    { name: "The", value: -12 },
    { name: "Magnetic" },
    { name: "Zeros", value: 37 }
  ]);

  const sorting = () => {
    const data = [...state];
    data.sort((a, b) =>
      a.name.toLowerCase().localeCompare(b.name.toLowerCase())
    );
    console.log(data);
    setState(data);
  };

  return (
    <div className="App">
      <table>
        <tr>
          <th>Firstname</th>
          <th>data</th>
        </tr>
        {state.map(i => {
          return (
            <tr key={i.name}>
              <td>{i.name}</td>
              <td>{i.value}</td>
            </tr>
          );
        })}
      </table>
      <button onClick={sorting}>sort</button>
    </div>
  );
}

You can play with the working example here: Stackblitz Link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.