Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to sort data getting from an endpoint in javascript using react?

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>
)
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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()));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!