Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
What is a fast sort algorithm for a very, very long array of objects in JavaScript?

I have been trying to sort an array with 2000 elements in ReactJS using JavaScript. The array looks like this:

data = [

         {
    index: 0,
    id: "404449",
    product_name: "ette",
    brand_name: "Dyrberg/Kern",
    base_price: "55.000",
    actual_price: "55.000",
    filename:
      "http://images.booztx.com/dyrbergkern/400x523/329679_ette_sg_crystal.jpg",
  },
  {
    index: 1,
    id: "414661",
    product_name: "braided mobile chain",
    brand_name: "Octopus",
    base_price: "44.900",
    actual_price: "44.900",
    filename: "http://images.booztx.com/octopus/400x523/SC09-750MU.jpg",
  },

       ]

I tried sorting it by base_price with Array.sort( ) of JavaScript, like this:

 data.sort((a, b) => {
     
      return parseFloat(a.base_price) - parseFloat(b.base_price);
    });

but since the array is very long, it has 2000 elements it takes a very long time to sort. It takes about 4 minutes. Does anyone have any solutions?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As mentioned in the comments, what you're describing should not actually take very long. Here's an example, and on my machine I'm seeing less than 10 milliseconds of runtime for a 2000 element array:

const createFakeProduct = () => {
  const priceNumber = Math.random() * 100;
  const priceString = priceNumber.toFixed(3);
  return {
    index: 0,
    id: "12345",
    product_name: "hello world",
    brand_name: "foo",
    base_price: priceString,
    actual_price: priceString,
    filename: "abc123.jpg",
  }
}

const data = [];
const len = 2000;
for (let i = 0; i < len; i++) {
  data.push(createFakeProduct());
}


const before = Date.now();
data.sort((a, b) => {
  return parseFloat(a.base_price) - parseFloat(b.base_price);
});
console.log(`elapsed: ${Date.now() - before} milliseconds`);

There must be something additional thing besides what you've shared that's slowing it down.

about 4 years ago · Juan Pablo Isaza Relatório

0

It turns out that the problem was using imports incorrectly. I imported the data as a constant, but array.sort( ) was trying to change it. I used a local variable for the result of sort and that fixed the problem.

import { data } from "assets/data/productList";

export const sortItems = (sortMode) => {
  const SoretdData = data.sort((a, b) => {
    if (sortMode === SortModes[1].type) {
      return parseFloat(a.base_price) - parseFloat(b.base_price);
    } else {
      return parseFloat(b.base_price) - parseFloat(a.base_price);
    }
  });
  return SoretdData;
};

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda