하면 주소 우리에게 당신이이 개 부분에서 지적한 문제를 보자
[A] 동일한 키를 가지고 있지만 다른 값을 갖는 문제 [Soln] 사용자 정의 클래스 즉, 아래 코드 스 니펫의 DataKeys를 선언하십시오.
[b] 목록에서 키 순서 지정 [Soln] 사용자 정의 클래스에 대한 IComperable 구현. 다음은
당신이 구현할 수있는 샘플 클래스는 다음과 같습니다
private static void Main(string[] args)
{
List<DataKeys> dataRepository = new List<DataKeys>()
{
new DataKeys(10, "Key-10"),
new DataKeys(11, "Key-11"),
new DataKeys(9, "Key-9"),
new DataKeys(8, "Key-8"),
new DataKeys(100, "Key-100")
};
dataRepository.Sort();
foreach (var dataKeyse in dataRepository)
{
Console.WriteLine(dataKeyse.Key);
}
}
출력 :
internal class DataKeys : IComparable<DataKeys>
{
private int key;
private string values;
public DataKeys(int key, string values)
{
this.key = key;
this.values = values;
}
internal int Key
{
get { return key; }
}
internal string Values
{
get { return Values; }
}
public int CompareTo(DataKeys other)
{
if (this.key > other.key) return 1;
else if (this.key < other.key) return -1;
else return 0;
}
}
그냥 확인하려면이 코드 샘플 클라이언트 코드를 기반으로 실행 얼마나
'IComparer'를 구현하십시오. [자습서를 보려면 여기를 참고하십시오] (http://www.codeproject.com/Articles/42839/Sorting-Lists-using-IComparable-and-IComparer-Inte). – Candide
as sidenote : .net 2.0을 타겟팅하는 동안 대부분의 C# 3.0 기능을 사용할 수 있습니다. – CodesInChaos