퇴근5분전


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; }
    }

'# 4) .Net ( Vs 2010 ) > C#' 카테고리의 다른 글

.Net] 버튼 Pressed Event!!  (0) 2012.05.09
.NET ] 멀티 랭귀지 지원 ...  (0) 2012.03.06
[IPC] Event 추가 ~~  (0) 2011.05.14
LINQ] 로또 구하기?  (1) 2011.04.25
[C#]Box 그리기...  (0) 2010.11.29