Mitul Panchal
Mitul Panchal

Reputation: 283

javascript sort array of string by value after special character

I want to sort by array which contains value having colon (:)

This is the below input

[
  'Severity',
  'Name',
  'U1A_Shift SCM: UPTT-Pressure (Bara)',
  'U1A_Shift SCM: DPTT-Pressure (Bara)',
  'U3B SCM: APTT-Pressure (Bara)',      
  'U3B SCM: UPTT-Pressure (Bara)',      
  'U1B SCM: DPTT-Pressure (Bara)',      
  'U1B SCM: UPTT-Pressure (Bara)',      
  'U3B SCM: DPTT-Pressure (Bara)',      
  'U1A_Shift SCM: UPTT-Temp (DegC)',    
  'U1A_Shift SCM: DPTT-Temp (DegC)',    
  'U3B SCM: APTT-Temp (DegC)',
  'U3B SCM: UPTT-Temp (DegC)',
  'U1B SCM: DPTT-Temp (DegC)',
  'U1B SCM: UPTT-Temp (DegC)',
  'U3B SCM: DPTT-Temp (DegC)',
  'U1B SCM: PCV-CHOKE status - Control position',
  'U3B SCM: PCV-CHOKE status - Control position',
  'U1A_Shift SCM: PCV-CHOKE status - Control position',
  'Alarms',
  'Advisories',
  '__row_index'
]

I want to sort / group it by the value after colon (:)

This should be below output

[
    'Severity',
    'Name': 'U3B',
    'U1A_Shift SCM: UPTT-Pressure (Bara)',    // grouped by UPTT-Pressure (Bara)
    'U3B SCM: UPTT-Pressure (Bara)',
    '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)',
    'U3B SCM: APTT-Pressure (Bara)', // grouped by APTT-Pressure (Bara)
    'U1A_Shift SCM: UPTT-Temp (DegC)', // grouped by UPTT-Temp (DegC)
    'U3B SCM: UPTT-Temp (DegC)',
    '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)',
    'U3B SCM: APTT-Temp (DegC)', // 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',
    'Advisories',
    '__row_index',
]

I need to sort this array value which lies after ":" Eg: APTT-Temp (DegC)

How can i sort / group array values

Any help would by highly appreciated :)

Upvotes: 1

Views: 74

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386680

You could collect all groups and single values and return a flat array.

const
    data = ['Severity', 'Name', 'U1A_Shift SCM: UPTT-Pressure (Bara)', 'U1A_Shift SCM: DPTT-Pressure (Bara)', 'U3B SCM: APTT-Pressure (Bara)', 'U3B SCM: UPTT-Pressure (Bara)', 'U3B SCM: DPTT-Pressure (Bara)', 'U1A_Shift SCM: UPTT-Temp (DegC)', 'U1A_Shift SCM: DPTT-Temp (DegC)', 'U3B SCM: APTT-Temp (DegC)', 'U3B SCM: UPTT-Temp (DegC)', 'U1B SCM: DPTT-Temp (DegC)', 'U1B SCM: UPTT-Temp (DegC)', 'U3B SCM: DPTT-Temp (DegC)', 'U1B SCM: PCV-CHOKE status - Control position', 'U3B SCM: PCV-CHOKE status - Control position', 'U1B SCM: DPTT-Pressure (Bara)', 'U1B SCM: UPTT-Pressure (Bara)', 'U1A_Shift SCM: PCV-CHOKE status - Control position', 'Alarms', 'Advisories', '__row_index'],
    map = data.reduce((m, s) => {
        const group = s.split(/:\s*/)[1] || m.size;
        return m.set(group, [...(m.get(group) || []), s]);
    }, new Map),
    grouped = Array.from(map.values()).flat(),
    counts = Array
        .from(map, ([k, { length }]) => [k, typeof k === 'string' && length])
        .filter(([, length]) => length);

console.log(counts);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions