Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sort an array of objects by 1 or more property values

I am figuring out if there is a better way to sort the below array.

The below code sorts the array based on the label and deviceRegistered.
if label is Desk phone and deviceRegistered is true then it should take the precedence.

My approach is like below.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const terminals = [
  {
    "device":"JKJCF00",
    "directory":"+1899990000",
    "label":"Jabber",
    "deviceRegistered":"false"
  },
  {
    "device":"IOP8999",
    "directory":"9099886644",
    "label":"Desk Phone",
    "deviceRegistered":"false"
  }, 
  {
    "device":"KLJ7888",
    "directory":"+8999999",
    "label":"Jabber",
    "deviceRegistered":"true"
  },  
  {
    "device":"VFD87987",
    "directory":"+12386444",
    "label":"Desk Phone",
    "deviceRegistered":"true"
  }]
let term = [...terminals],arr=[],sortedLines = [],lineObj ={};
  term.forEach(line => arr.indexOf(line.label)===-1 ? arr.push(line.label):'');
  arr.forEach(device => {
     let filterArr = term.filter(line => line.label === device)
     let sortArr = [...filterArr].sort((dev1,dev2) => dev1.deviceRegistered !== 'true' ? 1 : dev2.deviceRegistered !== 'true' ? -1 : 0)
     lineObj[device] = sortArr
  })
  for (line in lineObj){ console.log(lineObj[line])
     sortedLines.push(...lineObj[line])
  }
}

The output is

[
  {
    "device":"KLJ7888",
    "directory":"+8999999",
    "label":"Jabber",
    "deviceRegistered":"true"
  },
  {
    "device":"JKJCF00",
    "directory":"+1899990000",
    "label":"Jabber",
    "deviceRegistered":"false"
  },
  {
    "device":"VFD87987",
    "directory":"+12386444",
    "label":"Desk Phone",
    "deviceRegistered":"true"
  },
  {
    "device":"IOP8999",
    "directory":"9099886644",
    "label":"Desk Phone",
    "deviceRegistered":"false"
  }
]

>Solution :

You could check the properties and use the delta of the boolean values.

const
    array = [
        { device: "JKJCF00", directory: "+1899990000", label: "Jabber", deviceRegistered: "false" },
        { device: "IOP8999", directory: "9099886644", label: "Desk Phone", deviceRegistered: "false" },
        { device: "KLJ7888", directory: "+8999999", label: "Jabber", deviceRegistered: "true" },
        { device: "VFD87987", directory: "+12386444", label: "Desk Phone", deviceRegistered: "true" }
    ];
    
array.sort((a, b) =>
    (b.label === 'Desk Phone') - (a.label === 'Desk Phone') ||
    (b.deviceRegistered === 'true') - (a.deviceRegistered === 'true')
);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading