【问题标题】:Sort array of objects in JavaScript based on object keys substring基于对象键子字符串对 JavaScript 中的对象数组进行排序
【发布时间】:2025-08-29 15:45:02
【问题描述】:

我有一组对象

[
  {
    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,
  },
]

因此输出应该是

[
  {
    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,
  }
]

我需要根据“:”之后的键名对这个对象数组进行排序,例如:APTT-Temp (DegC)

我只列出了我收到的对象数组的单个对象

我需要对这个对象数组进行排序,基本上是按“:”(冒号)后面的key的值来分组

【问题讨论】:

标签: javascript arrays json


【解决方案1】:

您只需稍微修改以下 SO 问题的答案,以便它比较(例如整数)值而不是键 Sorting an array of objects by object key。 或者,您可以使用此 SO 问题的解决方案:How does sort function work in JavaScript, along with compare function

工作示例

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)

【讨论】:

    最近更新 更多