2011-03-15 74 views
0

我已經看過遍及互聯網,試圖找到如何解決以下問題的示例。如何在取消排序中按「值」排序列表並按「鍵」排序重複項?

如何在取消排序中按「值」排序列表並按「鍵」排序重複項? 然後以下列格式打印結果。

我已經附上我的代碼,它的工作原理,但是當您使用SortedList()時發生重複值時會發生問題。我非常感謝,如果有人可以請修改此代碼或以另一種方式準確地告訴我如何做到這一點,那就是快速而有效。

非常感謝您提前。

BEFORE排序:

價值的,關鍵的排序

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"); 

AFTER:

價值的,關鍵的

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

打印輸出結果:

blue> = red;

& & red> = dodBlue;

& & dodBlue> = green;

& & green> = yellow;

& & yellow> = black;

& & black> = cyan;

& & cyan> = orange;

& & orange> = white;

+0

,願意說的哈希表? – 2011-03-15 05:38:45

回答

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