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

180
Visualizações
How i can do bubble sort to array with object?

There is a task to make array sorting through Bubble sort. I just can’t understand how exactly it is necessary to implement sorting of such an array, in which objects. Can someone helpme, pls. I need sorting by data.year.

const [table, setTable] = useState([
  {
    text: "Пошел в свой первый класс",
    id: 0,
    data: {
      year: 2012,
      day: 25,
      month: 1,
    },
  },
  {
    text: "Поехал на чемпионат по бейсболу",
    id: 1,
    data: {
      year: 2018,
      day: 14,
      month: 3,
    },
  },
  {
    text: "Поступил в институт",
    id: 2,
    data: {
      year: 2007,
      day: 12,
      month: 4,
    },
  },
]);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Compare the year instead of object in bubble sort algorithm

//Bubble sort algorithm

const bubbleSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i; j++) {
      if (arr[j].data.year > arr[j + 1].data.year) { //Added check inside object "data.year"
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
};

const myArr = [
  {
    text: 'Пошел в свой первый класс',
    id: 0,
    data: {
      year: 2012,
      day: 25,
      month: 1,
    },
  },
  {
    text: 'Поехал на чемпионат по бейсболу',
    id: 1,
    data: {
      year: 2018,
      day: 14,
      month: 3
    }
  },
  {
    text: 'Поступил в институт',
    id: 2,
    data: {
      year: 2007,
      day: 12,
      month: 4
    },
  },
] 

console.log(bubbleSort(myArr))

about 4 years ago · Juan Pablo Isaza Relatório

0

This will return the sorted array

function bblSort(arr) {
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length - i - 1; j++) {
      if (arr[j].data.year > arr[j + 1].data.year) {
        var temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
  return(arr);
}


var arr = [
  {
    text: "Пошел в свой первый класс",
    id: 0,
    data: {
      year: 2012,
      day: 25,
      month: 1,
    },
  },
  {
    text: "Поехал на чемпионат по бейсболу",
    id: 1,
    data: {
      year: 2018,
      day: 14,
      month: 3,
    },
  },
  {
    text: "Поступил в институт",
    id: 2,
    data: {
      year: 2007,
      day: 12,
      month: 4,
    },
  },
];

about 4 years ago · Juan Pablo Isaza Relatório

0

Use JS's .sort to sort it e.g. ...useState([...].sort((item1, item2) => item1.data.year - item2.data.year)) it takes a comparison function as it's first argument, if you don't pass any function it will try to sort it lexicographically.

But still it will do quick or merge sort depending on the size of the array. If you want it to do a bubble sort for some reason you need to write the sort function manually.

About sorts: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#examples

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