0

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?

2 Answers 2

3

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)

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

Comments

1

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

2 Comments

check expected & your output
You can make the compare function more generic by passing the key by which it will be sorted. Assume it is not index key for bot the case then this compare function will fail

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.