0

I have an array in the following format. I have sorted the array in alphabetical order.But every element in the array, a sub array exist. I would like to sort them in alphabetical order too.

var myObj = [
{
    "name":"John",
    "items": [
        { "id":1, "car":"maruti" },  
        { "id":2, "car":"wolks" },
        { "id":3, "car":"bmw" }            
    ]
},
{
    "name":"Peter",
    "items": [
        { "id":4, "car":"alto" },  
        { "id":5, "car":"swift" },                          
    ]
}];

This is the code I have used to sort the main array. Here I would like to sort the 'items' array in alphabetical order.

myObj.sort(function(a, b) {
     var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();          
     if (nameA < nameB) {
         return -1
     }
     if (nameA > nameB){
         return 1
     }
     return 0 
}); 

3 Answers 3

3

It doesn't feel right to do anything in a sort function other than sort the immediate items in the array - I'd iterate twice, once to sort the myObj, and once to sort each items property.

You can also use localeCompare to simplify your code:

var myObj=[{"name":"John","items":[{"id":1,"car":"maruti"},{"id":2,"car":"wolks"},{"id":3,"car":"bmw"}]},{"name":"Peter","items":[{"id":4,"car":"alto"},{"id":5,"car":"swift"},]}]
myObj.sort((a, b) => a.name.localeCompare(b.name));
myObj.forEach(({ items }) => items.sort((a, b) => a.car.localeCompare(b.car)));
console.log(myObj);

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

Comments

0

You need to iterate over the array and then sort items of each object.

var myObj = [{"name":"John","items":[{"id":1,"car":"maruti"},{"id":2,"car":"wolks"},{"id":3,"car":"bmw"}]},{"name":"Peter","items":[{"id":4,"car":"alto"},{"id":5,"car":"swift"},]}];
      
myObj.forEach(({items}) => items.sort((a,b) => a.car.localeCompare(b.car)));
console.log(myObj);

Comments

0

You need to loop through all objects in array. You can use map function for that and sort each items list with a sort function.

var myObj = [
          {
          "name":"John",
          "items": [
              { "id":1, "car":"maruti" },  
              { "id":2, "car":"wolks" },
              { "id":3, "car":"bmw" }            
          ]},
          {
            "name":"Peter",
          "items": [
              { "id":4, "car":"alto" },  
              { "id":5, "car":"swift" },                          
          ]
       }
      ];
function sorting(a, b){
    // you can use just 
    // return a.car.localeCompare(b.car)  
    // reference CertainPerformance answer
    var nameA=a.car.toLowerCase(), nameB=b.car.toLowerCase();          
    if (nameA < nameB) {
      return -1
    }
    if (nameA > nameB){
      return 1
    }
    return 0 
}
myObj.forEach(obj => obj.items.sort(sorting))
console.log(myObj)
// myObj.map(obj => obj.items.sort(sorting)) // returns new array
// updated answer for useful comments.

2 Comments

Use forEach instead of map if you're not returning a new array.
@Barmar is right. map has very specific purpose: create new array with "mapped" items. It is not intented to do side-effects. There is forEach for that.

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.