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 array of objects with maximum of total of specific key values

I have an array of object as follows:

var tempListData = [
    {
        "driver": "467f0b0bc9a",
        "company": "9d85ccdcfc5",
        "driverName": "David Smith",
        "companyName": "Express",
        "CreateIdlingEvent": 0,
        "CreateSpeedingEvent": 2502,
        "CreateHarshAccelerationEvent": 1988,
        "CreateHarshDecelerationEvent": 1450
    },
    {
        "driver": "7fcc39deb8fc7",
        "company": "a2345ccdcfc5",
        "driverName": "John Doe",
        "companyName": "Express",
        "CreateSpeedingEvent": 2970,
        "CreateHarshAccelerationEvent": 2414,
        "CreateHarshDecelerationEvent": 1750,
        "CreateIdlingEvent": 0
    },
    {
        "driver": "c4e39006",
        "company": "9d8616dd",
        "driverName": "Henry",
        "companyName": "Express",
        "CreateIdlingEvent": 250,
        "CreateSpeedingEvent": 2300,
        "CreateHarshAccelerationEvent": 1988,
        "CreateHarshDecelerationEvent": 1450
    }
]

I need to sort the objects basis on the maximum total of CreateSpeedingEvent and CreateIdlingEvent and CreateHarshAccelerationEvent and CreateHarshDecelerationEvent values.

i.e. object with the maximum total values of "CreateIdlingEvent", CreateSpeedingEvent,CreateHarshAccelerationEvent, and CreateHarshDecelerationEvent that should become the first object in the array.

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

>Solution :

1) You can sort it using customised sorting comparator function

var tempListData = [
  {
    driver: "467f0b0bc9a",
    company: "9d85ccdcfc5",
    driverName: "David Smith",
    companyName: "Express",
    CreateIdlingEvent: 0,
    CreateSpeedingEvent: 2502,
    CreateHarshAccelerationEvent: 1988,
    CreateHarshDecelerationEvent: 1450,
  },
  {
    driver: "7fcc39deb8fc7",
    company: "a2345ccdcfc5",
    driverName: "John Doe",
    companyName: "Express",
    CreateSpeedingEvent: 2970,
    CreateHarshAccelerationEvent: 2414,
    CreateHarshDecelerationEvent: 1750,
    CreateIdlingEvent: 0,
  },
  {
    driver: "c4e39006",
    company: "9d8616dd",
    driverName: "Henry",
    companyName: "Express",
    CreateIdlingEvent: 250,
    CreateSpeedingEvent: 2300,
    CreateHarshAccelerationEvent: 1988,
    CreateHarshDecelerationEvent: 1450,
  },
];

const getSum = (o) =>
  o.CreateIdlingEvent +
  o.CreateSpeedingEvent +
  o.CreateHarshAccelerationEvent +
  o.CreateHarshDecelerationEvent;

const result = [...tempListData].sort((a, b) => getSum(b) - getSum(a));

console.log(result);

2) You can first create a map of sum using Map and then sort it using customised sorting function as:

var tempListData = [
  {
    driver: "467f0b0bc9a",
    company: "9d85ccdcfc5",
    driverName: "David Smith",
    companyName: "Express",
    CreateIdlingEvent: 0,
    CreateSpeedingEvent: 2502,
    CreateHarshAccelerationEvent: 1988,
    CreateHarshDecelerationEvent: 1450,
  },
  {
    driver: "7fcc39deb8fc7",
    company: "a2345ccdcfc5",
    driverName: "John Doe",
    companyName: "Express",
    CreateSpeedingEvent: 2970,
    CreateHarshAccelerationEvent: 2414,
    CreateHarshDecelerationEvent: 1750,
    CreateIdlingEvent: 0,
  },
  {
    driver: "c4e39006",
    company: "9d8616dd",
    driverName: "Henry",
    companyName: "Express",
    CreateIdlingEvent: 250,
    CreateSpeedingEvent: 2300,
    CreateHarshAccelerationEvent: 1988,
    CreateHarshDecelerationEvent: 1450,
  },
];

const map = new Map();
tempListData.forEach((o) =>
  map.set(
    o.driver,
    o.CreateIdlingEvent +
      o.CreateSpeedingEvent +
      o.CreateHarshAccelerationEvent +
      o.CreateHarshDecelerationEvent
  )
);

const result = [...tempListData].sort((a, b) => map.get(b.driver) - map.get(a.driver));

console.log(result);
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