androidGenX
androidGenX

Reputation: 1158

sort json nested array based on index not working as expected

I have a json array and I want to sort the array based on its index number

[  
   {  
      "name":"abc",
      "index":2,
      "values":[  
         {  
            "work":"three3",
            "index":3
         },
         {  
            "work":"one1",
            "index":1
         },
         {  
            "work":"two2",
            "index":2
         }
      ]
   },
   {  
      "name":"pqr",
      "index":1,
      "values":[  
         {  
            "work":"three",
            "index":3
         },
         {  
            "work":"two",
            "index":2
         },
         {  
            "work":"one",
            "index":1
         }
      ]
   }
]

What I expect from this array is:

[  
   {  
      "filename":"pqr",
      "children":[  
         {  
            "work":"one",
            "index":1
         },
         {  
            "work":"two",
            "index":2
         },
         {  
            "work":"three",
            "index":3
         }
      ]
   },
   {  
      "filename":"abc",
      "children":[  
         {  
            "work":"one1",
            "index":1
         },
         {  
            "work":"two2",
            "index":2
         },
         {  
            "work":"three3",
            "index":3
         }
      ]
   }
   ]

Tried something like below.

const filterBy = (arr, childname, filterText) =>
{
    return arr.map(({filename, children}) =>
    {
        return {filename, children: children.map(({filename, children}) =>
        {
           if (filename === childname)
               return {filename, children: children.filter(
                   x => x.filename.match(filterText)
               )};
           else
               return {filename, children};
        })};
    });
}

It is a json array and what we can not be sure it will be in order so I want an array or object should be in sorted order

But how can I include that index in inner level and outer level and sort using it accordingly?

Upvotes: 0

Views: 74

Answers (2)

RahulB
RahulB

Reputation: 2110

First sort the outer array, use same function to sort inner array

function compare(a, b) {
  if (a.index < b.index)
    return -1;
  if (a.index > b.index)
    return 1;
  return 0;
}

var objs = [{
    "name": "abc",
    "index": 2,
    "values": [{
        "work": "three3",
        "index": 3
      },
      {
        "work": "one1",
        "index": 1
      },
      {
        "work": "two2",
        "index": 2
      }
    ]
  },
  {
    "name": "pqr",
    "index": 1,
    "values": [{
        "work": "three",
        "index": 3
      },
      {
        "work": "two",
        "index": 2
      },
      {
        "work": "one",
        "index": 1
      }
    ]
  }
]


var result = objs.sort(compare).map(function(item) {
  var children = item.values.sort(compare);
  return {
    filename: item.name,
    children: children
  }
});

console.log(result);

Upvotes: 1

brk
brk

Reputation: 50316

You can first sort the array based on index. This will return a sorted array now use map. Inside the callback function get the values array and again sort it.Inside the array map function return the object with required key and value

let data = [{
    "name": "abc",
    "index": 2,
    "values": [{
        "work": "three3",
        "index": 3
      },
      {
        "work": "one1",
        "index": 1
      },
      {
        "work": "two2",
        "index": 2
      }
    ]
  },
  {
    "name": "pqr",
    "index": 1,
    "values": [{
        "work": "three",
        "index": 3
      },
      {
        "work": "two",
        "index": 2
      },
      {
        "work": "one",
        "index": 1
      }
    ]
  }
]


let newdt = data.sort(function(a, b) {
  return a.index - b.index
}).map(function(item) {
  let val = item.values.sort(function(a, b) {
    return a.index - b.index;
  })

  return {
    name: item.name,
    children: val
  }
})
console.log(newdt)

Upvotes: 3

Related Questions