2011-03-15 4 views
0

나는 인터넷을 통해 다음을 수정하는 방법의 예를 찾으려고 노력했다."값"으로 목록을 정렬하고 "키"로 중복을 정렬하는 방법?

"값"으로 정렬 순서가 잘못된 목록을 정렬하고 "키"로 중복을 정렬하는 방법은 무엇입니까? 그런 다음 결과를 아래의 형식으로 인쇄하십시오.

내 코드를 묶었지만 작동하지만 SortedList()를 사용할 때 발생하는 값이 중복되면 문제가 발생합니다. 누군가가이 규범을 수정하거나 정확하게 다른 방법으로 이것을 보여줄 수있는 방법을 제시 할 수 있다면, 나는 그것을 신속히 찬성 할 것입니다. 그것은 빠르고 능률적입니다.

대단히 감사합니다.

전 SORT :

VALUE의, KEY의

sL.Add(1269.63,"white"); 
sL.Add(1270.36,"orange"); 
sL.Add(1272.06,"yellow"); 
sL.Add(1271.50,"cyan"); 
sL.Add(1272.06,"black"); 
sL.Add(1274.12,"dodBlue"); 
sL.Add(1276.02,"blue"); 
sL.Add(1273.21,"green"); 
sL.Add(1275.52,"red"); 

SORT AFTER :

VALUE의

, KEY의

sL.Add(1276.02,"blue"); 
sL.Add(1275.52,"red"); 
sL.Add(1274.12,"dodBlue"); 
sL.Add(1273.21,"green"); 
sL.Add(1272.06,"black"); 
sL.Add(1272.06,"yellow"); 
sL.Add(1271.50,"cyan"); 
sL.Add(1270.36,"orange"); 
sL.Add(1269.63,"white"); 

CODE :

SortedList sL = new SortedList(); 

sL.Add(SMA(8)[0], "white"); 
sL.Add(SMA(10)[0], "orange"); 
sL.Add(SMA(13)[0], "yellow"); 
sL.Add(SMA(20)[0], "cyan"); 
sL.Add(SMA(30)[0], "black"); 
sL.Add(SMA(40)[0], "dodBlue"); 
sL.Add(SMA(50)[0], "blue"); 
sL.Add(SMA(100)[0], "green"); 
sL.Add(SMA(200)[0], "red"); 

Print(" " + " " + sL.GetByIndex(8) + " " + ">=" + " " + sL.GetByIndex(7)); 
Print("&&" + " " + sL.GetByIndex(7) + " " + ">=" + " " + sL.GetByIndex(6)); 
Print("&&" + " " + sL.GetByIndex(6) + " " + ">=" + " " + sL.GetByIndex(5)); 
Print("&&" + " " + sL.GetByIndex(5) + " " + ">=" + " " + sL.GetByIndex(4)); 
Print("&&" + " " + sL.GetByIndex(4) + " " + ">=" + " " + sL.GetByIndex(3)); 
Print("&&" + " " + sL.GetByIndex(3) + " " + ">=" + " " + sL.GetByIndex(2)); 
Print("&&" + " " + sL.GetByIndex(2) + " " + ">=" + " " + sL.GetByIndex(1)); 
Print("&&" + " " + sL.GetByIndex(1) + " " + ">=" + " " + sL.GetByIndex(0)); 
,515,

PRINT OUT 결과 :

블루> = 빨간색;

& & 적색> = dodBlue;

& & dodBlue> = 녹색;

& & 녹색> = 노란색;

& & 황색> = 블랙;

& & 블랙> = 시안;

& & 시안> = 오렌지;

& & 오렌지> = 백색;

+0

유 싶지 해시 말합니까? –

답변

0

왜이 용도로 LINQ를 사용하지 않습니까? OrderBy() 및 Distinct() 메소드가 도움이 될 것입니다.

0
다음과 같이 당신은 LINQ와 함께이 작업을 수행 할 수 있습니다

:

 Dictionary<int, string> lst = new Dictionary<int,string>(10); 

     lst.Add(7, "MAX"); 
     lst.Add(2, "A"); 
     lst.Add(1, "A"); 
     lst.Add(8, "0"); 

     Dictionary<int, string> newList = (lst.OrderBy(x => x.Value).ThenBy(x => x.Key)).ToDictionary(x => x.Key,x=>x.Value); 
관련 문제