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

129
Views
¿Cómo ordenar los datos de la matriz en el orden en que aparecen para la coincidencia exacta?

Necesito filtrar los datos según el primer carácter coincidente o cadena de esa palabra e imprimir el resultado como se muestra a continuación.

  1. Entrada: 'T' o 'Th' o 'The' Salida: ['The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight', 'Dora the explorer']

  2. Entrada: 'Da' Salida:['Dark Knight', 'The Dark Knight']

En el primer caso, 'T/Th/The' es la coincidencia exacta de ('The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight'). Sí, Dora la exploradora también es válida ya que 'T/Th/The' es parte de la cadena, pero necesito llevarla hasta el final.

La lógica de lo que escribí devuelve ["Dora la exploradora", "La redención de Shawshank", "El padrino", "El padrino: Parte II", "El caballero oscuro"] que no es correcta. Lo mismo se aplica para el segundo también. No estoy seguro de qué hice mal aquí.

 const fruits = ['Dora the explorer', 'The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight', 'Dark Knight']; const character = "The"; const filteredNames = fruits.filter((item) => { const subStrLength = character.length; return item.split(' ').some(el => el.slice(0, subStrLength).toLowerCase() === character.toLowerCase()) }).sort((a, b) => a.toLowerCase().indexOf(character) - b.toLowerCase().indexOf(character)) console.log(filteredNames)

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Aquí hay un enfoque. Mientras clasificamos, verificamos si ambas películas para clasificar comienzan con el personaje y las almacenamos en isGoodMatchA y isGoodMatchB , si una de ellas es falsa y la otra verdadera, entonces le damos prioridad a la verdadera. De lo contrario, si ambos son verdaderos o falsos, hacemos una localeCompare simple para encontrar cuál viene primero.

 const fruits = ['Dora the explorer', 'The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight', 'Dark Knight']; const character = "The"; const filteredNames = fruits .filter((item) => item.toLowerCase().includes(character.toLowerCase())) .sort((a, b) => { const key = character.toLowerCase(); const isGoodMatchA = a.toLowerCase().startsWith(key); const isGoodMatchB = b.toLowerCase().startsWith(key); if (isGoodMatchA ^ isGoodMatchB) { // XOR return isGoodMatchA ? -1 : 1; } return a.localeCompare(b); }); console.log(filteredNames)

Array.prototipo.incluye()

String.prototype.startsWith()

about 4 years ago · Santiago Gelvez 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!