ArrayList.Sort 하기...
ArrayList 에 담아 있는 데이타를 순서에 맞춰 정렬하는 부분에 대해 구현한 것임.
간단하다... 아래처럼 하면
간단히 담는놈(ArrayList)에 담겨질놈(SortunitClass)을 담아서,
정렬할 방법을 가지고 있는 놈( SortunitClassCompare )에게 넘겨준다.
// 훈스에 올라온 질문글에 대한 답글로 단 소스임.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ArrayList ar = new ArrayList();
ar.Add(new SortunitClass() { Index = 9, Data = "아홉" });
ar.Add(new SortunitClass() { Index = 3, Data = "셋" });
ar.Add(new SortunitClass() { Index = 5, Data = "다섯" });
ar.Add(new SortunitClass() { Index = 11, Data = "열하나" });
ar.Add(new SortunitClass() { Index = 1, Data = "하나" });
ar.Sort(new SortunitClassCompare());
}
}
public class SortunitClassCompare : IComparer, IComparer<SortunitClass>
{
public int Compare(SortunitClass x, SortunitClass y)
{
return x.Index.CompareTo(y.Index);
}
public int Compare(object x, object y)
{
return Compare((SortunitClass)x, (SortunitClass)y);
}
}
public struct SortunitClass
{
public int Index { get; set; }
public string Data { get; set; }
}