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

161
Views
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 answers
Answer question

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 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!