I need your help. I have one js file with a code in which i have one component - Home
. In this component i am trying to take objects from array users
and sort them in ascending and descending order using select
tag and options. I am trying to do it, but i still don't manage to do in. Can you help me: how to sort this array and how to render in page this object depending on the order? Thank you very much
import React, {useState} from "react";
export let Home = () => {
const users = [
{id: 1, name: "One"},
{id: 2, name: "Two"},
{id: 3, name: "Three"}
];
const [arrayToTop] = useState(users);
const [arrayToBottom] = useState(users);
arrayToTop.sort((a,b) => {
return a.id - b.id;
})
arrayToBottom.sort((a,b) => {
return b.id - a.id;
})
return (<div>
<select>
<option value={arrayToTop}>To top</option>
<option value={arrayToBottom}>To bottom</option>
</select>
</div>)
}