sort an array with react hooks
Asked Answered
R

3

8

I'm doing a simple sort of an array with react hooks, but its not updating state. Can anyone point out what I'm going here?

import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const dogs = [{ name: "fido", age: 22 }, { name: "will", age: 50 }];

function App() {
  const [dogList, setDogList] = useState(dogs);

  const sortByAge = () => {
    const sorted = dogList.sort((a, b) => {
      return b.age - a.age;
    });
    setDogList(sorted);
    alert(sorted[0].name);
  };

  return (
    <div>
      {dogs.map((dog, i) => {
        return <p key={i}>{dog.name}</p>;
      })}
      <div onClick={sortByAge}>sort by age</div>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Remediless answered 24/9, 2019 at 20:43 Comment(1)
do you have a code example?Remediless
A
17

First: because Array.prototype.sort() is in place, clone before you sort:

const sorted = [...dogList].sort((a, b) => {
  return b.age - a.age;
});

Without this the change detection will not detect the array changed.

You call map on dogs. Fix:

  return (
    <div>
      {dogList.map((dog, i) => {
        return <p key={i}>{dog.name}</p>;
      })}
      <div onClick={sortByAge}>sort by age</div>
    </div>
  );

Demo: https://jsfiddle.net/acdcjunior/kpwb8sq2/1/

Armand answered 24/9, 2019 at 20:47 Comment(4)
I knew it was something related to this ty!Remediless
@Armand I'm having the same problem. I tried your solution but it didn't work. Woud you mind taking a look at my Stack post #75668291Machutte
Can you explain why spreading the state into an array with the spread operator ([...dogList].sort) is different from just using dogList.sort?Underslung
@Underslung because the .sort methods sorts "in place", meaning it changes the original array. Doing [...dogList] creates a clone of the original array and then sorts the clone. This leaves the original array unmodified.Armand
P
3

const dogs = [{ name: "fido", age: 22 }, { name: "will", age: 50 }];
class Application extends React.Component {
  state = {dogList: dogs};

  sortByAge = () => {
    const sorted = this.state.dogList.sort((a, b) => {
      return b.age - a.age;
    });
    this.setState({dogList: sorted});
  };

  render() {
    return (
      <div>
        {this.state.dogList.map((dog, i) => {
          return <p key={i}>{dog.name}</p>;
        })}
        <button onClick={this.sortByAge}>sort by age</button>
      </div>
    );
  }
}
React.render(<Application />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.min.js"></script>
<div id="root"></div>

This is the code using hooks

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

const dogs = [{ name: "fido", age: 22 }, { name: "will", age: 50 }];
function Application() {
  const [dogList, setDogList] = useState(dogs);
  const sortByAge = () => {
    const sorted = dogList.sort((a, b) => {
      return b.age - a.age;
    });
    setDogList(sorted);
  };
  return (
    <div>
      {dogList.map((dog, i) => {
        return <p key={i}>{dog.name}</p>;
      })}
      <button onClick={sortByAge}>sort by age</button>
    </div>
  );
}

ReactDOM.render(<Application />, document.getElementById('root'));
Pandemonium answered 24/9, 2019 at 21:15 Comment(4)
Nice answer. Just add some explanation and context next time.Isfahan
Why did you provide an answer using a class component when the request was for a solution using hooks? (Your answer is not wrong btw)Cortico
@evolutionxbox. because I want to show you a live example with code snippet. so I use class component. but if you want I will provide example which uses the hooks.Pandemonium
No no that’s fine. It just seemed odd to me that’s allCortico
A
1

sort by name example...

const dogs = [{ name: "bido", age: 22 }, { name: "aill", age: 50 }, { name: "test", age: 90 }];

function App() {
  const [dogList, setDogList] = React.useState(dogs);
  function compare(a, b) 
{
  if (a.name > b.name) return 1;
  if (a.name < b.name) return -1;
  return 0;
}
  const sortByAge = () =>
{

    const sorted = [...dogList].sort(compare);//calling compare function
    setDogList(sorted);//storing sorted values
    
  };

  return (
    <div>
      {dogList.map((dog, i) => {
        return <p key={i}>{dog.name}{dog.age}</p>;
      })}
      <div onClick={sortByAge}>sort by age</div>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Acth answered 20/4, 2022 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.