Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace SortApplication
{
Public class getList
{
Public int id;
Public int parentId;
Public string name;
Public getList (int id, int parentId, string name)
{
This. id = id;
This. parentId = parentId;
This. name = name;
}
}
Class Program
{
Private static IList <getList> GetList (getList [] myList, int pid)
{
Int count = myList. Length;
IList <getList> al = new List <getList> ();
For (int I = 0; I <count; I ++)
{
If (myList [I]. parentId = pid)
{
// Console. WriteLine ("test above:" + pid );
Al. Add (myList [I]);
}
}
Return al;
}
Private static void SortList (getList [] myList, IList <getList> Al, ref getList [] listCopy)
{
If (listCopy [0] = null)
{
Al. Add (myList [0]);
}
Try
{
Foreach (getList lis in Al)
{
Int I;
For (I = 0; I <listCopy. Length; I ++)
{
If (listCopy [I] = null)
{
Break;
}
}
// Console. WriteLine ("//" + I + "//////");
ListCopy [I] = (getList) lis;
// Console. WriteLine (f );
Al = GetList (myList, lis. id );
SortList (myList, Al, ref listCopy );
}
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
Static void Main (string [] args)
{
GetList [] ourList = new getList [] {
New getList (1, 0, ""),
New getList (2, 1, "B "),
New getList (3, 1, "c "),
New getList (4, 2, "d "),
New getList (5, 4, "e "),
New getList (7, 3, "aaa "),
New getList (6, 5, "f ")
};
String BlankBase = new string ('', 8 );
Console. WriteLine ("sorting previous objects ");
For (int I = 0; I <ourList. Length; I ++)
{
Console. WriteLine (ourList [I]. id + BlankBase + ourList [I]. parentId + BlankBase + ourList [I]. name );
}
GetList [] listCopy = new getList [ourList. Length];
IList <getList> Alist = new List <getList> ();
SortList (ourList, Alist, ref listCopy );
Console. WriteLine ("objects sorted by \ r \ n ");
For (int I = 0; I <listCopy. Length; I ++)
{
Try
{
Console. WriteLine (listCopy [I]. id + BlankBase + listCopy [I]. parentId + BlankBase + listCopy [I]. name );
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
}
}
}
From the wangganggang90 Column