I want to sort the items according to their Id like in ascending so how to do it?
heres the code from react component i used...
import { useState, useEffect} from "react"
//styles
import './ItemList.css'
export default function ItemList() {
const[Items,setItems]=useState([])
useEffect(() => {
fetch('http://localhost:3000/data')
.then(response=>response.json())
.then(json=>setItems(json))
}, [])
console.log(Items)
return (
<div className="item-list">
<h1>Item List</h1>
<ul>
{Items.map(data=>(
<li key={data.id}>
<h2>{data.id}</h2>
<h3>{data.name}</h3>
</li>
))}
</ul>
</div>
)
}
Pagination might create problems if you sort on the front end.
use array sort for frontend, I'm assuming id is number
fetch("http://localhost:3000/data")
.then((response) => response.json())
.then((json) => setItems(json.sort((a, b) => a.id - b.id)));
If it's a string
fetch("http://localhost:3000/data")
.then((response) => response.json())
.then((json) => setItems(json.sort()));