Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda