-2

I've got array of objects

[
  {
    Severity: "<span class='tableActive'></span>",
    Name: 'U3B',
    'U1A_Shift SCM: UPTT-Pressure (Bara)': '-',
    'U1A_Shift SCM: DPTT-Pressure (Bara)': '-',
    'U3B SCM: APTT-Pressure (Bara)': '3510.00',
    'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
    'U1B SCM: DPTT-Pressure (Bara)': '-',
    'U1B SCM: UPTT-Pressure (Bara)': '-',
    'U3B SCM: DPTT-Pressure (Bara)': '740.00',
    'U1A_Shift SCM: UPTT-Temp (DegC)': '-',
    'U1A_Shift SCM: DPTT-Temp (DegC)': '-',
    'U3B SCM: APTT-Temp (DegC)': '1565.00',
    'U3B SCM: UPTT-Temp (DegC)': '2654.00',
    'U1B SCM: DPTT-Temp (DegC)': '-',
    'U1B SCM: UPTT-Temp (DegC)': '-',
    'U3B SCM: DPTT-Temp (DegC)': '3159.00',
    'U1B SCM: PCV-CHOKE status - Control position': '-',
    'U3B SCM: PCV-CHOKE status - Control position': '-',
    'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
    Alarms: 0,
    Advisories: 0,
    __row_index: 0,
  },
]

Hence output should be

[
  {
    Severity: "<span class='tableActive'></span>",
    Name: 'U3B',
    'U1A_Shift SCM: UPTT-Pressure (Bara)': '-',    // grouped by UPTT-Pressure (Bara)
    'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
    'U1B SCM: UPTT-Pressure (Bara)': '-',
    'U1A_Shift SCM: DPTT-Pressure (Bara)': '-',    //grouped by DPTT-Pressure (Bara)
    'U1B SCM: DPTT-Pressure (Bara)': '-',
    'U3B SCM: DPTT-Pressure (Bara)': '740.00',
    'U3B SCM: APTT-Pressure (Bara)': '3510.00', // grouped by APTT-Pressure (Bara)
    'U1A_Shift SCM: UPTT-Temp (DegC)': '-', // grouped by UPTT-Temp (DegC)
    'U3B SCM: UPTT-Temp (DegC)': '2654.00',
    'U1B SCM: UPTT-Temp (DegC)': '-',
    'U1A_Shift SCM: DPTT-Temp (DegC)': '-', // grouped by DPTT-Temp (DegC)
    'U1B SCM: DPTT-Temp (DegC)': '-',
    'U3B SCM: DPTT-Temp (DegC)': '3159.00',
    'U3B SCM: APTT-Temp (DegC)': '1565.00', // grouped by APTT-Temp (DegC)
    'U1B SCM: PCV-CHOKE status - Control position': '-', // grouped by PCV-CHOKE status - Control position
    'U3B SCM: PCV-CHOKE status - Control position': '-',
    'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
    Alarms: 0,
    Advisories: 0,
    __row_index: 0,
  }
]

I need to sort this array of objects based on key name which lies after ":" Eg: APTT-Temp (DegC)

I'm listing only single object I receive array of objects though

I need to sort this array of objects, basically group it by the value of key after ":"(colon)

biberman
  • 5,606
  • 4
  • 11
  • 35
Mitul Panchal
  • 143
  • 2
  • 13

1 Answers1

1

You just have to modify the answer to the following SO question a bit so that it compares the (e.g. integer) values instead of the keys Sorting an array of objects by object key. Alternatively you can use the solution for this SO question: How does sort function work in JavaScript, along with compare function

Working example:

var arr = [
  {
    Severity: "<span class='tableActive'></span>",
    Name: 'U3B',
    'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
    'U3B SCM: APTT-Temp (DegC)': '1565.00',
    'U3B SCM: PCV-CHOKE status - Control position': '-',
    'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
    Alarms: 0,
    Advisories: 0,
    __row_index: 0
  },
  {
    Severity: "<span class='tableActive'></span>",
    Name: 'U3B',
    'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
    'U3B SCM: APTT-Temp (DegC)': '840.00',
    'U3B SCM: PCV-CHOKE status - Control position': '-',
    'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
    Alarms: 0,
    Advisories: 0,
    __row_index: 0
  },
  {
    Severity: "<span class='tableActive'></span>",
    Name: 'U3B',
    'U3B SCM: UPTT-Pressure (Bara)': '3413.00',
    'U3B SCM: APTT-Temp (DegC)': '1427.00',
    'U3B SCM: PCV-CHOKE status - Control position': '-',
    'U1A_Shift SCM: PCV-CHOKE status - Control position': '-',
    Alarms: 0,
    Advisories: 0,
    __row_index: 0
  }
];

arr.sort( function(a, b) {
  const aValue = parseInt(a['U3B SCM: APTT-Temp (DegC)']);
  const bValue = parseInt(b['U3B SCM: APTT-Temp (DegC)']);
  return aValue - bValue;
});

console.log(arr)
biberman
  • 5,606
  • 4
  • 11
  • 35