You basically need a sort of topological sort.
We use the standard method of merging lists. I write in C #, LinkedList cannot merge into it, so I wrote my bike.
Auxiliary classes:
class LinkedNode<T> { public T Entry; public LinkedNode<T> Next; } class MergeableLinkedList<T> { public LinkedNode<T> First, Last; public MergeableLinkedList(T head) { First = new LinkedNode<T>() { Entry = head }; Last = First; } public void Merge(MergeableLinkedList<T> other) // other is destroyed { Last.Next = other.First; Last = other.Last; } }
Your data structure:
class Entry { public int id; public int? parent; }
Data:
var data = new Entry[] { new Entry() { id = 1 }, new Entry() { id = 2, parent = 1 }, new Entry() { id = 3 }, new Entry() { id = 4, parent = 1 }, new Entry() { id = 5, parent = 3 }, new Entry() { id = 6, parent = 4 } };
Sorting:
// ΡΠΎΠ·Π΄Π°ΡΠΌ ΡΠΏΠΈΡΠΊΠΈ var lists = new Dictionary<int, MergeableLinkedList<Entry>>(); foreach (var d in data) lists[d.id] = new MergeableLinkedList<Entry>(d); // ΡΠ»ΠΈΠ²Π°Π΅ΠΌ ΠΈΡ
foreach (var d in data) { // Π΅ΡΠ»ΠΈ Π΅ΡΡΡ ΠΏΡΠ΅Π΄ΠΎΠΊ... if (d.parent != null) { var parentId = d.parent.Value; // ΡΠ²ΠΎΠΉ ΡΠΏΠΈΡΠΎΠΊ ΠΏΠΎΠ΄ΡΠ΅ΠΏΠ»ΡΠ΅ΠΌ Π² ΠΊΠΎΠ½Π΅Ρ ΡΠΏΠΈΡΠΊΠ° ΠΏΡΠ΅Π΄ΠΊΠ° lists[parentId].Merge(lists[d.id]); lists[d.id] = lists[parentId]; } } // ΠΈ ΠΏΠ΅ΡΠ΅ΠΏΠ°ΠΊΠΎΠ²ΡΠ²Π°Π΅ΠΌ Π²ΡΡ Π² Π½ΠΎΠ²ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² var newData = new Entry[data.Length]; var newDataIndex = 0; foreach (var d in data) { if (d.parent == null) { for (var curr = lists[d.id].First; curr != null; curr = curr.Next) newData[newDataIndex++] = curr.Entry; } }
Conclusion:
foreach (var d in newData) { if (d.parent != null) Console.Write(d.parent + "."); Console.WriteLine(d.id); }
Result:
1 1.2 1.4 4.6 3 3.5
[5.1, 4, 3.2, 2, 1.1, 1].sort()good? - br3t