3

I have one array, which includes children. I have successfully sorted the array but unable to sort it, children.

Using the below code I am able to sort outer mail array elements but not its children.

https://jsfiddle.net/eokd5uzj/

var arry = [{
    'id': 301,
    'name': '2 Foo',
    'open': 'open',
    'children': [{
        'id': 1313,
        'name': '2.1 Foo ',
        'open': 'open'
      },
      {
        'id': 1143,
        'name': '2.3 Foo ',
        'open': 'open'
      },
      {
        'id': 1132,
        'name': '2.2 Foo ',
        'open': 'open'
      },
    ],
  },
  {
    'id': 30,
    'name': '1 Foo',
    'open': 'open',
    'children': [{
        'id': 1134,
        'name': '1.1 Foo ',
        'open': 'open'
      },
      {
        'id': 1130,
        'name': '1.3 Foo ',
        'open': 'open'
      },
      {
        'id': 1123,
        'name': '1.2 Foo ',
        'open': 'open'
      },
    ],
  },
];


function SortByName(a, b) {
  var aName = a.name.toLowerCase();
  var bName = b.name.toLowerCase();
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}


$(document).ready(function() {
  var sorted_array = arry.sort(SortByName)
  console.log(sorted_array)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id='data'>

</p>

6
  • 3
    You never call .sort on the children. Commented May 7, 2020 at 7:41
  • stackoverflow.com/questions/5503900/… Commented May 7, 2020 at 7:42
  • @VLAZ ok then what is solution Commented May 7, 2020 at 7:42
  • @c.grey call .sort(SortByName) on the children of each element. Commented May 7, 2020 at 7:43
  • @MakwanaPrahlad No, it is not recursive Commented May 7, 2020 at 7:43

4 Answers 4

4

You need to iterate the array as well for sorting all children. An assignment is not necessary, because Array#sort mutates the array.

array.forEach(({ children }) => children.sort(sortByName));
Sign up to request clarification or add additional context in comments.

Comments

1

Please modify your function as below

function SortByName(a, b){
	if(a.children){
  	a.children = a.children.sort(SortByName)
  }
  if(b.children){
  	b.children = b.children.sort(SortByName)
  }
  var aName = a.name.toLowerCase();
  var bName = b.name.toLowerCase();
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

6 Comments

Best solution so far!
@c.grey i didn't do anything... just called your function itself........ :)
no, it's a worst solution, because the children are sorted for every parent element over and over.
@c.grey the complexity of the sorting witll be WAY higher by repeatedly sorting. Heck, based on the sorting algorithm implementation, you might get the worst complexity as algorithms like quicksort have worst performance on an already sorted dataset. Even if that's avoided, it's completely unnecessary to sort and re-sort the children.
@VLAZ do you have any optimal solution.
|
0
var updated_sorted_array = sorted_array.map(function(arr){
  arr.children = arr.children.sort(SortByName);
  return arr;
})

1 Comment

next time please add information and explanation on why and how
0

you need 2 functions to sort these objects, one for Desc and one for Asc sort like this:

function sortByKeyDesc(array, key) {
    return array.sort(function (a, b) {
        var x = a[key]; var y = b[key];
        return ((x > y) ? -1 : ((x < y) ? 1 : 0));
    });
}
function sortByKeyAsc(array, key) {
    return array.sort(function (a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

And when you want to sort your objects you can pass in the original collection and the Column that you need to sort based on like this:

  function querySucceeded(data) {
     posts = [];
     posts = sortByKeyDesc(data, "TagName");
  }

1 Comment

You never need to implement two functions from scratch for sorting. To sort in reverse order, all you need is to negate the result of the correct order, so a generic reverseSort = sortingFn => (...args) => -1 * sortingFn(...args) works. Or you can make it more concrete by just flipping the arguments reverseSort = fn => (a, b) => fn(a, b). If you have a functional utility library, you can just use a ready made solution sortAsc = { /* ... logic ... */ }; sortDesc = flip(sortAsc); This way you don't have to repeat yourself.

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.