1

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 :)

13
  • The desired output is a syntax error? Commented May 4, 2021 at 13:53
  • Nothing about your expected results, looks ordered to me. Commented May 4, 2021 at 13:57
  • @evolutionxbox : sorry man initially it was object, i've updated the desired output as arrays now. :) Commented May 4, 2021 at 13:57
  • Please also note that neither the input nor the output are valid JSON. Commented May 4, 2021 at 13:57
  • 1
    What's the order? It looks random Commented May 4, 2021 at 13:59

1 Answer 1

2

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; }

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Nina, what changed i might have to do in order to achieve this stackoverflow.com/questions/67369165/…
you could build a new object with the order of the grouped keys from above.
could you plz help me with this section, I need it urgently :)
@MitulPanchal Something like this?
@NinaScholz yes what about object's value how we can fetch it for(const key of grouped) { obj[key] = 'whatever'; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.