1

I have an array of objects as below. I want to recursively sort this based on the key values

{
      "settingsList": [
        {
          "category1": [
            {
              "categoryName": "DRIVER",
              "description": "DRIVER",
              "sequence": 1
            },
            {
              "categoryName": "BALL",
              "description": "BALL",
              "sequence": 2
            },
            {
              "categoryName": "SAMPLE",
              "description": "SAMPLE",
              "sequence": 3
            },
            {
              "categoryName": "USER",
              "description": "USER",
              "sequence": 4
            }
          ]
        },
        {
          "category2": [
            {
              "paramName": "CPP",
              "description": "CPP",
              "sequence": 1
            },
            {
              "paramName": "PP",
              "description": "PP",
              "sequence": 2
            },
            {
              "paramName": "MP",
              "description": "MP",
              "sequence": 3
            }
          ]
        }
        {
          "source": {
            "instanceName": "instance_1"
          }
        }
      ]
    }

How can I efficiently sort recursively to display it sorted alphabetically on the key values. Expected output:

{
      "settingsList": [
        {
          "category": [
            {
              "categoryName": "BALL",
              "description": "BALL",
              "sequence": 2
            },
            {
              "categoryName": "DRIVER",
              "description": "DRIVER",
              "sequence": 1
            },
            {
              "categoryName": "SAMPLE",
              "description": "SAMPLE",
              "sequence": 3
            },
            {
              "categoryName": "USER",
              "description": "USER",
              "sequence": 4
            }
          ]
        },
        {
          "category2": [
            {
              "paramName": "CPP",
              "description": "CPP",
              "sequence": 1
            },
            {
              "paramName": "MP",
              "description": "MP",
              "sequence": 3
            },
            {
              "paramName": "PP",
              "description": "PP",
              "sequence": 2
            }
          ]
        },
        {
          "source": {
            "instanceName": "instance_1"
          }
        }
      ]
    }

below is the sample code that was tried

var object = {
      "settingsList": [
        {
          "category1": [
            {
              "categoryName": "DRIVER",
              "description": "DRIVER",
              "sequence": 1
            },
            {
              "categoryName": "BALL",
              "description": "BALL",
              "sequence": 2
            },
            {
              "categoryName": "SAMPLE",
              "description": "SAMPLE",
              "sequence": 3
            },
            {
              "categoryName": "USER",
              "description": "USER",
              "sequence": 4
            }
          ]
        },
        {
          "category2": [
            {
              "paramName": "CPP",
              "description": "CPP",
              "sequence": 1
            },
            {
              "paramName": "PP",
              "description": "PP",
              "sequence": 2
            },
            {
              "paramName": "MP",
              "description": "MP",
              "sequence": 3
            }
          ]
        },
        {
          "source": {
            "instanceName": "instance_1"
          }
        }
      ]
    }

var keys = Object.keys(object);

var sortedKeys = keys.sort((key1, key2)=>{
    key1 = key1.toLowerCase();
    key2 = key2.toLowerCase();
    if(key1 < key2) return -1;
    if(key1 > key2) return 1;
    return 0;
})

 function sortData(object){
    var newObject = {},
        keys = Object.keys(object);
        
   keys.sort(function(key1, key2){
        key1 = key1.toLowerCase();
        key2 = key2.toLowerCase();
        if(key1 < key2) return -1;
        if(key1 > key2) return 1;
        return 0;
    });


  for(var index in keys){
        var key = keys[index];
        if(typeof object[key] == 'object' && !(object[key] instanceof Array)){
            newObject[key] = sortData(object[key]);
        } else {
            newObject[key] = object[key];
        }
    }

    return newObject;
}

var sortedData=sortData(object)

console.log(sortedData)

....................................................................................................................................................................................................................................................................

5
  • 2
    You should add the code you've already attempted to your question. Commented Apr 17, 2021 at 6:46
  • So, you wish to sort all the category arrays based on description? What have you tried so far? Commented Apr 17, 2021 at 6:55
  • not on desc, but on the categoryName for category1, paramName for category2 and so on Commented Apr 17, 2021 at 7:09
  • I've you find the solution ? Is my answer helped you ? Commented Apr 18, 2021 at 5:44
  • this one is sorting by the keys only.. i want to sort by values of the keys.. I m trying to modify it to accomodate Commented Apr 18, 2021 at 5:49

2 Answers 2

1

What you tried to achieved is called deep sort.

You can use deep-sort-object library as follow :

var sortobject = require('deep-sort-object');
var sortedData = sortobject(object);
console.log(sortedData);

Or if you don't want to use a library, you can use this gist as reference.

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

Comments

1

Here is an iterative solution using object-scan

// const objectScan = require('object-scan');

const myData = { settingsList: [{ category1: [{ categoryName: 'DRIVER', description: 'DRIVER', sequence: 1 }, { categoryName: 'BALL', description: 'BALL', sequence: 2 }, { categoryName: 'SAMPLE', description: 'SAMPLE', sequence: 3 }, { categoryName: 'USER', description: 'USER', sequence: 4 }] }, { category2: [{ paramName: 'CPP', description: 'CPP', sequence: 1 }, { paramName: 'PP', description: 'PP', sequence: 2 }, { paramName: 'MP', description: 'MP', sequence: 3 }] }, { source: { instanceName: 'instance_1' } }] };

const sort = (data) => {
  const logic = {
    'settingsList[*].category1': (value) => value.sort((a, b) => a.categoryName.localeCompare(b.categoryName)),
    'settingsList[*].category2': (value) => value.sort((a, b) => a.paramName.localeCompare(b.paramName))
  };

  objectScan(Object.keys(logic), {
    filterFn: ({ value, matchedBy }) => {
      matchedBy.forEach((needle) => logic[needle](value));
    }
  })(data);
};

console.log(sort(myData));
// => undefined

console.log(myData);
// => { settingsList: [ { category1: [ { categoryName: 'BALL', description: 'BALL', sequence: 2 }, { categoryName: 'DRIVER', description: 'DRIVER', sequence: 1 }, { categoryName: 'SAMPLE', description: 'SAMPLE', sequence: 3 }, { categoryName: 'USER', description: 'USER', sequence: 4 } ] }, { category2: [ { paramName: 'CPP', description: 'CPP', sequence: 1 }, { paramName: 'MP', description: 'MP', sequence: 3 }, { paramName: 'PP', description: 'PP', sequence: 2 } ] }, { source: { instanceName: 'instance_1' } } ] }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.