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

115
Vistas
Sorting arrays in JavaScript

I received a big array of object from the backend and I used filtering to leave only these values

const markets = ['IT', 'DE', 'UK', 'FR', 'NL', 'US'] // so now this is what have

I am going then to map through markets, but it will always start by IT and follows that order. Is there a way to sort this following a specific order?

['US', 'DE', 'UK', 'FR', 'IT', 'NL']

I want to sort every array that i receive following this specific order even if I don't receive all the values.

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

0

One could implement a function which uses an object as lookup for each of a market's precedence value and which in addition places unknown market items last.

function getAssuredMarketPrecedence(marketList) {
  const marketPrecedence = {
    US: 0, DE: 1, UK: 2, FR: 3, IT: 4, NL: 5,
  };
  return Array
    .from(marketList)
    .sort((a, b) =>
      (marketPrecedence[a] ?? 1000)  - (marketPrecedence[b] ?? 1000)
    );
}
const markets = ['IT', 'DE', 'unknown2', 'UK', 'FR', 'unknown1', 'NL', 'US']
const sorted = getAssuredMarketPrecedence(markets);

console.log({ markets, sorted });
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to write your own custom compare function

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Smth like this:

markets = ['IT', 'DE', 'UK', 'FR', 'NL', 'US']

markets.sort((item1, item2) => {
    neededOrder = ['US', 'DE', 'UK', 'FR', 'IT', 'NL']
    idx1 = neededOrder.indexOf(item1)
    idx2 = neededOrder.indexOf(item2)
    if(idx1 < idx2) {
        return -1
    }
    else if(idx1 > idx2) {
        return 1
    } else {
        return 0
    }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can assign each market a value within an object, and use that to compare and sort against the market array you receive from the backend:

const markets = ['IT', 'DE', 'UK', 'FR', 'NL', 'US']

let mappedMarkets = {
  'US': 1,
  'DE': 2,
  'UK': 3,
  'FR': 4,
  'IT': 5,
  'NL': 6
}

console.log(markets.sort((a, b) => mappedMarkets[a] - mappedMarkets[b]))

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