How to sort the array of objects in javascript?

stackoverflow_user

Hi i want to sort an array of objects in javascript. Below is the example data.

const example = [
    {
        name: "c_name",
        children: [{
            name: "child",
            email: "[email protected]",
            children: [{
                name: "nested_child",
                email: "[email protected]",
            }]
        }]
    },
    {
        name: "a_name",
        children: [{
            name: "some_name",
            email: "[email protected]",
            children: []
        }]
    },
    {
        name: "name",
        children: [{
            name: "child_name",
            email: "[email protected]",
            children: []
        }]
    }
];

Should sort this array based on property 'name' and the children object should be sorted again based on 'name' property.

So the expected output is like below, and would like to retain other properties as well like email property in children.

a_name some_name c_name child nested_child name child_name

What i have done...i have a sort function that sorts the array by name property. however dont know how to sort the children object with name property.

const sorted_example = example.sort(this.sort_by_name());

sort_by_name = () => {
return (a, b) => {
    let result;
    const a_value = a.name.toLowerCase();
    const b_value = b.name.toLowerCase();
    if (a_value > b_value) {
        result = 1;
    } else if (a_value < b_value) {
        result = -1;
    } else {
        result = 0;
    }

    return result;
};

};

Could someone help me how to continue with this. thanks.

HMR

The previous answers got you there most of the way but you need to sort again if an item has children. In my example I don't mutate the original array (use .slice to make a shallow copy so .sort doesn't mutate).

const example = [{"name":"c_name","children":[{"name":"child","email":"[email protected]","children":[{"name":"nested_child","email":"[email protected]"}]},{"name":"b"},{"name":"a"}]},{"name":"a_name","children":[{"name":"some_name","email":"[email protected]","children":[]}]},{"name":"name","children":[{"name":"child_name","email":"[email protected]","children":[]}]}];

const sortRecursive = (data) => {
  const recur = (arr) =>
    arr
      .slice()
      .sort((a, b) => a.name.localeCompare(b.name))
      //check each item to see if it has children that is an array
      .map(
        (item) =>
          //if item has children that is an array then sort that
          //  and it's childrens childrens children
          Array.isArray(item.children)
            ? {
                ...item,
                children: recur(item.children),
              }
            : item, //no children, just return the item
      );
  return recur(data);
};
//note that sortRecursive does not change example but returns a new array
//  that is sorted
console.log(sortRecursive(example));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to group and sort an array of objects

How can I sort a javascript array of objects numerically and then alphabetically?

How to sort an array of objects by date?

How to sort an array of objects with jquery or javascript

How to sort an array of objects in Java?

Sort array of objects with another in javascript

Sort array of objects of objects in javascript

Sort array of objects in javascript with two sort conditions

How to sort a nested array of objects in javascript?

How to sort an array in JavaScript or jQuery with multiple objects per array record

how to sort array of objects in mongodb

How to sort Javascript objects in an array using regex

Sort by multiple objects in a array in Javascript

Sort an array of objects dynamically javascript

Javascript sort array of objects using array of priority

Javascript sort array of objects on key

Javascript sort an objects by another array

How to bubble sort objects in array?

How to sort a multidimensional array of objects by property value in javascript?

Javascript Array of Objects Sort

JavaScript sort an array of objects based array of properties

How to sort array of objects in Javascript by their keys in order?

How to sort an array of objects then map through it in JavaScript?

How to sort javascript array of objects, based upon a key

How to sort an array of objects (classes)?

How to sort an array with parents and children objects with JavaScript?

How to sort array of objects according to epoch time in JavaScript

How can I "multi sort" an array of objects in Javascript by keys

How to sort javascript's array of objects by multiple property value