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

224
Visualizações
Merge Sort, sorting dates properly but not integer values

I am doing a coding challenge trying to learn merge sort and I have gotten my merge sort to handle dates properly but not an integer value. Currently it seems to outputting data at random.

EDIT: There are over a dozen different posts like the sample data listed. I am unable to sort each of those objects based on their votes from high to low

My Merge Sort Function:

function sortBy(array, key, descending = false) {
  const length = array.length;
  if (length === 1) {
    return array;
  } else if (length === 2) {
    const aValue = array[0][key];
    const bValue = array[1][key];
    if (bValue > aValue) {
      return array;
    }
    return [
      array[0],
      array[1],
    ];
  }

  const mid = Math.floor(length / 2);
  const firstHalf = array.slice(0, mid);
  const secondHalf = array.slice(mid, length);

  const arrayOne = sortBy(firstHalf, key);
  const arrayTwo = sortBy(secondHalf, key);

  const merged = [];
  while (arrayOne.length || arrayTwo.length) {
    if (!arrayOne.length) {
      merged.push(arrayTwo.shift());
      continue;
    }

    if (!arrayTwo.length) {
      merged.push(arrayOne.shift());
      continue;
    }

    const valueOne = arrayOne[0][key];
    const valueTwo = arrayTwo[0][key];
    if (valueOne <= valueTwo) {
      merged.push(arrayOne.shift());
    } else if (valueTwo < valueOne) {
      merged.push(arrayTwo.shift());
    }
  }

  return descending ? merged.reverse() : merged;
}

Sample Data

    [{
    created: '2016-03-07T05:24:40.340Z',
    details: 'Right now we only support single backticks. Would be nice to do triple as well... Consider supporting more or all of markdown but I\'m not sure that\'s the right direction.',
    title: 'Support triple backtick codeblocks',
    votes: 17,
  },]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

2 things I see just reading through the code:

  1. need to access different elements of the array in the last return statement
const length = array.length;
  if (length === 1) {
    return array;
  } else if (length === 2) {
    const aValue = array[0][key];
    const bValue = array[1][key];
    if (bValue > aValue) {
      return array;
    }
    return [
      array[0], // needs to be array[1]
      array[1], // needs to be array[0]
    ];
  }
  1. You need to be passing the third argument down everytime you recurse the function

    const arrayOne = sortBy(firstHalf, key, descending);

    const arrayTwo = sortBy(secondHalf, key, descending);

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