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

318
Views
Typescript javascript : custom sort on object array

I am looking for a typescript code snippet to get a custom order of the object array, by listing objects in order by type fiction, romance, other, drama, comedy.

I've tried .find() and push(); .sort(); but did not feel that its the right way.

please advise

the above question doesn't solve my use case. stackoverflow.com/questions/6913512/… its not a straight sort. I need it to sort by custom order fiction romance, other, drama, comedy.

  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "type": "fiction",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "type": "drama",
    "completed": false
  },
  {
    "userId": 1,
    "id": 3,
    "title": "fugiat veniam minus",
    "type": "fiction",
    "completed": false
  },
  {
    "userId": 1,
    "id": 4,
    "title": "et porro tempora",
    "type": "comedy",
    "completed": true
  },
  {
    "userId": 1,
    "id": 5,
    "title": "laboriosam mollitia et enim quasi adipisci quia provident illum",
    "type": "fiction",
    "completed": false
  },
  {
    "userId": 1,
    "id": 6,
    "title": "qui ullam ratione quibusdam voluptatem quia omnis",
    "type": "romance",
    "completed": false
  },
  {
    "userId": 1,
    "id": 7,
    "title": "illo expedita consequatur quia in",
    "type": "other",
    "completed": false
  }```
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can do:

const data = [{userId: 1,id: 1,title: "delectus aut autem",type: "fiction",completed: false,},{userId: 1,id: 2,title: "quis ut nam facilis et officia qui",type: "drama",completed: false,},{userId: 1,id: 3,title: "fugiat veniam minus",type: "fiction",completed: false,},{userId: 1,id: 4,title: "et porro tempora",type: "comedy",completed: true,},{userId: 1,id: 5,title: "laboriosam mollitia et enim quasi adipisci quia provident illum",type: "fiction",completed: false,},{userId: 1,id: 6,title: "qui ullam ratione quibusdam voluptatem quia omnis",type: "romance",completed: false,},{userId: 1,id: 7,title: "illo expedita consequatur quia in",type: "other",completed: false}]

const order = ['fiction', 'romance', 'other', 'drama', 'comedy']
const dataHash = data.reduce((a, c) => {
  a[c.type] = a[c.type] || { array: [] }
  a[c.type].array.push(c)
  return a
}, {})
const result = order.map(o => dataHash[o].array).flat()

console.log(result)

about 4 years ago · Santiago Gelvez 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!