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

231
Views
Merge Sort, ordenando las fechas correctamente pero no los valores enteros

Estoy haciendo un desafío de codificación tratando de aprender a ordenar por combinación y obtuve mi orden por combinación para manejar las fechas correctamente pero no un valor entero. Actualmente parece generar datos al azar.

EDITAR: Hay más de una docena de publicaciones diferentes como los datos de muestra enumerados. No puedo ordenar cada uno de esos objetos en función de sus votos de mayor a menor

Mi función de clasificación de combinación:

 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; }

Data de muestra

 [{ 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 answers
Answer question

0

2 cosas que veo solo leyendo el código:

  1. necesita acceder a diferentes elementos de la matriz en la última declaración de retorno
 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. Debe pasar el tercer argumento cada vez que repite la función

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

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

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!