Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
Sort object based on Enum List

I have an Object with multiple properties and want to sort based on specific enum list.

Below is Object

const ArratOfobj = [
   { id: 3, ShortType: "LocationWorn", ImageType: "B" },
   { id: 2, ShortType: "SipStillLife", ImageType: "D" },
   { id: 1, ShortType: "SipStillWorn", ImageType: "M" },
   { id: 4, ShortType: "LocationLife", ImageType: "M" },
];

Enum Order

  const order= {
            '0': { ShortType: "SIP Still Life", ImageType: "M"},
            '1': { ShortType: "SIP Still Life", ImageType: "B" },
            '2': { ShortType: "Location Still Life", ImageType: "M" },
            '3': { ShortType: "Location Still Life", ImageType: "B" }          
        }

**Expected Output **

[
   { id: 2, ImageType: "D", ShortType: "SipStillLife" }, 
   { id: 4, ImageType: "M", ShortType: "LocationLife" }, 
   { id: 1, ImageType: "M", ShortType: "SipStillWorn" }, 
   { id: 3, ImageType: "B", ShortType: "LocationWorn" }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can't create an object by this way, but I guess you meant an array ob objects.

there is how you could sort this array by using enum order :

      const ArratOfobj = [
          { id: 3, ShortType: "LocationWorn", ImageType: "B" },
          { id: 2, ShortType: "SipStillLife", ImageType: "D" },
          { id: 1, ShortType: "SipStillWorn", ImageType: "M" },
          { id: 4, ShortType: "LocationLife", ImageType: "M" },
        ];
        
        const enum Order {
          SipStillLife = 0,
          LocationLife = 1,
          SipStillWorn = 2,
          LocationWorn = 3,
        }
        
        const sortedArray = ArratOfobj.sort((a, b) => {
          return Order[a.ShortType] - Order[b.ShortType];
        });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda