Home > OS >  Sort array of objects with maximum of total of specific key values
Sort array of objects with maximum of total of specific key values

Time:11-26

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.

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related