By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: System Components - JavaScript task

+4 votes

You will be given a register of systems with components and subcomponents. You need to build an ordered database of all the elements that have been given to you.

The elements are registered in a very simple way. When you have processed all of the input data, you must print them in a specific order. For every System you must print its components in a specified order, and for every Component, you must print its Subcomponents in a specified order.

The Systems you’ve stored must be ordered by amount of components, in descending order, as first criteria, and by alphabetical order as second criteria. The Components must be ordered by amount of Subcomponents, in descending order.

The input comes as array of strings. Each element holds data about a system, a component in that system, and a subcomponent in that component. If the given system already exists, you should just add the new component to it. If even the component exists, you should just add the new subcomponent to it. The subcomponents will always be unique. The input format is:

“{systemName} | {componentName} | {subcomponentName}”

All of the elements are strings, and can contain any ASCII character. The string comparison for the alphabetical order is case-insensitive.


SULS | Main Site | Home Page
SULS | Main Site | Login Page
SULS | Main Site | Register Page
SULS | Judge Site | Login Page
SULS | Judge Site | Submittion Page
Lambda | CoreA | A23
SULS | Digital Site | Login Page
Lambda | CoreB | B24
Lambda | CoreA | A24
Lambda | CoreA | A25
Lambda | CoreC | C4
Indice | Session | Default Storage
Indice | Session | Default Security

|||Main Site
||||||Home Page
||||||Login Page
||||||Register Page
|||Judge Site
||||||Login Page
||||||Submittion Page
|||Digital Site
||||||Login Page
||||||Default Storage
||||||Default Security

As output you need to print all of the elements, ordered exactly in the way specified above.

The format is:

asked in JavaScript category by user sam

1 Answer

+2 votes

Creating a sorting function with two criteria might seem a bit daunting at first, but it can be simplified to the following: 

  •  If elements a and b are different based on the first criteria, then that result is the result of the sorting function, checking the second criteria is not required.
  • If elements a and b are equal based on the first criteria, then the result of comparing a and b on the second criteria is the result of the sorting.

my JS answer:

function system(array) {
    let map = new Map();
    for (let arr of array) {
        [systemName, componentName, subcomponentName] = arr.split(' \| ');

        if (!map.has(systemName)) {
            map.set(systemName, new Map());
        if (!map.get(systemName).has(componentName)) {
            map.get(systemName).set(componentName, []);

        let current = map.get(systemName).get(componentName);
        map.get(systemName).set(componentName, current);

    [].sort(function (a, b) {
        let a2 = a[0];
        let b2 = b[0];
        let valuea2 = [...a[1]].length;
        let valueb2 = [...b[1]].length;

        if (valueb2 > valuea2) {
            return 1;
        } else if (valueb2 < valuea2) {
            return -1;
        } else if (a2 > b2) {
            return 1;
        } else if (a2 < b2) {
            return -1;

        else return 0;
    }).forEach(([systemName, componentName])=> {

        [...componentName].sort(function (a, b) {
            let length = a[1].length;
            let length1 = b[1].length;
            return length1 - length;
        }).forEach(([k,v])=> {
            console.log(`|||` + k);
            for (let obj of v) {
                console.log(`||||||` + obj);

    ['SULS | Main Site | Home Page',
        'SULS | Main Site | Login Page',
        'SULS | Main Site | Register Page',
        'SULS | Judge Site | Login Page',
        'SULS | Judge Site | Submittion Page',
        'Lambda | CoreA | A23',
        'SULS | Digital Site | Login Page',
        'Lambda | CoreB | B24',
        'Lambda | CoreA | A24',
        'Lambda | CoreA | A25',
        'Lambda | CoreC | C4',
        'Indice | Session | Default Storage',
        'Indice | Session | Default Security']);
answered by user samfred5830