Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
I want to sort my words from arr by 2nd letter of each word according to my given pattern

/* 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

I want to sort my words from arr by 2nd letter of each word according to my given pattern.

['aobcdh','aiabch','obijkl']

#output should be: obijkl aiabch aobcdh */

// I tried this way:

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 = ['aiabch', 'aobcdh', 'obijkl', 'apcsdef', 'leeeeeeeeeeeeeeee']
let refArr = []
for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < pattern.length; j++) {
        if (pattern[j] === arr[i][1]) {
            refArr.push(j)
        }

    }
}
console.log(refArr)

// now what next?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use the sort method and the indexOf function

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 = ['aiabch', 'aobcdh', 'obijkl', 'apcsdef', 'leeeeeeeeeeeeeeee']

const newArr = arr.sort((a, b) => pattern.indexOf(a[1]) - pattern.indexOf(b[1]))
console.log(newArr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could generate an object with the order values and sort by this values.

If necessary convert the key to lower case and/or take a default value for sorting unknown letters to front or end.

const
    pattern = 'ebcdifghojklmnupqrstavwxyz',
    array = ['aobcdh', 'aiabch', 'obijkl'],
    order = Object.fromEntries(Array.from(pattern, (l, i) => [l, i + 1]));

array.sort((a, b) => order[a[1]] - order[b[1]]);

console.log(array);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Also, you can try this if you does not know Array methods.

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'], arr = ['aiabch', 'aobcdh', 'obijkl', 'apcsdef', 'leeeeeeeeeeeeeeee'], refArr = []

arr.forEach(a => pattern.includes(a[2]) ? refArr.push(a) : null)
console.log(refArr)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda