I am trying to sort the following data:
var data = [
{ path: '/a', order: 0 },
{ path: '/b', order: 1 },
{ path: '/a/d', order: 1 },
{ path: '/a/c', order: 0 }
];
Into:
var expected = [
{ path: '/a', order: 0 },
{ path: '/a/c', order: 0 },
{ path: '/a/d', order: 1 },
{ path: '/b', order: 1 }
];
I found a great post about hierarchical data:
What are the options for storing hierarchical data in a relational database?
From that question I think I am using a flat table:
A modification of the Adjacency List that adds a Level and Rank (e.g. ordering) column to each record.
The link in that SO question is down so I can't get more information from that.
I setup a JsFiddle to assert my sort function:
I already tried a few approaches but none of them gave the correct result.
A completely different solution would be to add the order and sort alphabetically. But since the result will end up as urls it's kinda ugly to have www.example.com/01-products/01-snowboard.
Update
I created a more complex JSFiddle example to show what I am trying to do:
So I am trying to sort on path but the nodes should be sorted on order at their level. So like a file system when you sort alphabetically but the last child is sorted on order.