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

157
Views
JS sort array object by custom key and value

I am fresh JS student and I found a problem which I can't sole by my self. I've got an array of objects and one of the key of these objects is 'priority' and values for that key are: "low", "mid", "high". I would like to sort my objects in array from the lowest priority to the highest, but I can't figure it out.

let thingsToDo = [
  {
  name: 'Call',
  priority: 'low',
},
  {
  name: 'Cook',
  priority: 'low',
  },
    {
  name: 'Eat',
  priority: 'high',
  },
                {
  name: 'Sleep',
  priority: 'mid',
  }
];

I was trying to make some custom sort function, but i don't know how to do it. What I think would be a good solution is giving these values some number and then sort it. Very important for me is to leave these values as a string. There has to be priority: "low"

I've been looking for solution sometime and I didn't found it.

I have no idea how to do it, so if there is some good person with a bigger experience, I beg for help.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can create an object that maps them to comparable values and then sort based on the them:

const priority = {
    low: 1,
    mid: 2,
    high: 3,
};

thingsToDo.sort(
    (a, b) => priority[a.priority] - priority[b.priority]
);

Here is a working snippet:

let thingsToDo = [
    {
        name: 'Call',
        priority: 'low',
    },
    {
        name: 'Cook',
        priority: 'low',
    },
    {
        name: 'Eat',
        priority: 'high',
    },
    {
        name: 'Sleep',
        priority: 'mid',
    },
];

const priority = {
    low: 1,
    mid: 2,
    high: 3,
};

thingsToDo.sort(
    (a, b) => priority[a.priority] - priority[b.priority]
);

console.log(thingsToDo)

about 4 years ago · Juan Pablo Isaza Report

0

You can place all the priorities in an array and sort the object's based on the index of their priority property in that array.

let thingsToDo = [
  {
  name: 'Call',
  priority: 'low',
},
  {
  name: 'Cook',
  priority: 'low',
  },
    {
  name: 'Eat',
  priority: 'high',
  },
                {
  name: 'Sleep',
  priority: 'mid',
  }
];
let priorities = ['low', 'mid', 'high'];
thingsToDo.sort((a, b)=>priorities.indexOf(a.priority) - priorities.indexOf(b.priority));
console.log(thingsToDo);

about 4 years ago · Juan Pablo Isaza Report

0

try it

const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort();

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!