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

115
Views
Solo quiero ordenar mis palabras de la matriz alfabéticamente por la primera letra y luego por la segunda letra de acuerdo con mi patrón dado que coincide con la primera letra

♦ patrón: e,b,c,d,i,f,g,h,o,j,k,l,m,n,u,p,q,r,s,t,a,v,w,x ,y,z

Quiero ordenar mis palabras de arr alfabéticamente por la primera letra y luego por la segunda letra de una palabra similar que coincida con la primera letra de acuerdo con mi patrón dado.

['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd']

♦ la salida debe ser:

['aibcdh', 'aobcdh', 'aabcdh', 'aacbdh', 'cighjd', 'cfghjd' ]

♦ o: aibcdh aobcdh aabcdh aacbdh cighjd cfghjd

Mi código aquí:

 let pattern = ['e', 'b', 'c', 'd', 'i', 'f', 'g', 'h', 'o', 'j', 'k', 'l', 'm', 'n', 'u', 'p', 'q', 'r', 's', 't', 'a', 'v', 'w', 'x', 'y', 'z'] let arr = ['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd'] let arrSorted = arr.sort() //Natural sorting console.log(arrSorted) // output in array const newArr = arrSorted.sort((a, b) => pattern.indexOf(a[1]) - pattern.indexOf(b[1])) console.log(newArr) //Sorted by its 2nd character with given pattern // single output without array for (let i = 0; i < pattern.length; i++) { for (let j = 0; j < arrSorted.length; j++) { if (pattern[i] === arrSorted[j][1]) { console.log(arrSorted[j]) //Sorted by its 2nd character with given pattern } } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede ordenar el primero por cadena y el segundo por valor personalizado.

 const pattern = 'ebcdifghojklmnupqrstavwxyz', array = ['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd'], order = Object.fromEntries(Array.from(pattern, (l, i) => [l, i + 1])); array.sort((a, b) => a[0].localeCompare(b[0]) || order[a[1]] - order[b[1]] ); console.log(array);

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!