简体   繁体   中英

Sort an array of objects dynamically javascript

I am having an array with the below items. I need to sort the below array to the array that is shown in sorted items,so that all the value with the rules can be together and the ELIG_DATABASE should be grouped with the ELIG_SERVICE.

 const items =[{"name":"ELIG_DATABASE","ready":true},
               {"name":"ELIG_RULES_SERVICE","ready":true},                   
               {"name":"ELIG_GATEWAY","ready":true}, 
               {"name":"ELIG_GATEWAY_LATEST","ready":true,"latest":true}, 
               {"name":"ELIG_SERVICE_LATEST","ready":true,"latest":true}, 
               {"name":"ELIG_SERVICE","ready":true}, 
               {"name":"HDXTS","ready":false},
               {"name":"RULES_VERSION","ready":true},];

I want to achieve this array so that values in the name property that has rules can be together,gateway things should be together, elig service thing should be together just that ELIG_DATABASE should be grouped together with elig service and then all other values in the name property can be sorted alphabetically.

         const sortedItems =[
               {"name":"ELIG_GATEWAY","ready":true},                    
            {"name":"ELIG_GATEWAY_LATEST","ready":true,"latest":true}, 
               {"name":"ELIG_RULES_SERVICE","ready":true},
               {"name":"RULES_VERSION","ready":true},
               {"name":"ELIG_DATABASE","ready":true},
               {"name":"ELIG_SERVICE_LATEST","ready":true,"latest":true}, 
               {"name":"ELIG_SERVICE","ready":true}, 
               {"name":"HDXTS","ready":false}
               ];

I tried using this code but that sorts alphabetically putting ELIG_DATABASE in first position.Could any one please help on how to achieve this array in minimum code as possible.

             items.sort((svcA, svcB) => {
             const serviceA = svcA.name.toUpperCase();
             const serviceB = svcB.name.toUpperCase();
             return serviceA.localeCompare(serviceB);
                });

You could take the wanted groups first in an array, sort the data and assign the object to the group or to the end of a temp array and get the flat data as result.

 var data = [{ name: "ELIG_DATABASE", ready: true }, { name: "ELIG_RULES_SERVICE", ready: true }, { name: "ELIG_GATEWAY", ready: true }, { name: "ELIG_GATEWAY_LATEST", ready: true, latest: true }, { name: "ELIG_SERVICE_LATEST", ready: true, latest: true }, { name: "ELIG_SERVICE", ready: true }, { name: "HDXTS", ready: false }, { name: "RULES_VERSION", ready: true }], together = [['GATEWAY'], ['RULES'], ['ELIG_DATABASE', 'ELIG_SERVICE']], groups = { GATEWAY: [], RULES: [], ELIG_DATABASE: [] }, temp = [groups.GATEWAY, groups.RULES, groups.ELIG_DATABASE], result; for (let o of data.sort(({ name: a }, { name: b }) => a.localeCompare(b))) { let target = together.find(a => a.some(v => o.name.includes(v))); if (target) groups[target[0]].push(o); else temp.push(o); } result = temp.flat(); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM