R
Ralph Wiggum
I'm trying to use a comparer to sort keys according to a specific priority. After adding the keys,
there is a KeyNotFoundException when I try to read values back from the dictionary. Even if the key is
in the SortedDictionary.Keys collection. Please help!
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
private void butSort_Click(object sender, EventArgs e)
{
DokumentSort ds = new DokumentSort();
SortedDictionary<string, string> dict = new SortedDictionary<string, string>(ds);
//only "pri1" is valid and should be placed first in dict
string[] sData = new string[] { "noPri0", "noPri1", "noPri2", "pri1" };
for (int i = 0; i < sData.Length; i++) {
if(dict.ContainsKey(sData+"")) continue;
dict.Add(sData+"", "anything"+i);
Debug.WriteLine("Added "+sData + " anything" + i);
}
foreach (string skey in dict.Keys){
//will fail here
Debug.WriteLine(skey + " " + dict[skey+""]);
}
}
public class DokumentSort : IComparer<string>{
Dictionary<string, int> dictPriority = new Dictionary<string, int>();
public DokumentSort(){
//Setup key priority. If unknown key, it should be placed last
string[] sOrder = new string[] { "pri0", "pri1", "pri2" };
for (int i = 0; i < sOrder.Length; i++) {
dictPriority.Add(sOrder+"", i);
}
}
public int Compare(string sX, string sY){
//Debug.WriteLine("Comparing "+sX + " " + sY);
if (sX == sY) return 0;
if(!dictPriority.ContainsKey(sX+"")) return 1;
if(!dictPriority.ContainsKey(sY+"")) return -1;
int priX = dictPriority[sX+""];
int priY = dictPriority[sY+""];
return priX-priY;
}
}
there is a KeyNotFoundException when I try to read values back from the dictionary. Even if the key is
in the SortedDictionary.Keys collection. Please help!
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
private void butSort_Click(object sender, EventArgs e)
{
DokumentSort ds = new DokumentSort();
SortedDictionary<string, string> dict = new SortedDictionary<string, string>(ds);
//only "pri1" is valid and should be placed first in dict
string[] sData = new string[] { "noPri0", "noPri1", "noPri2", "pri1" };
for (int i = 0; i < sData.Length; i++) {
if(dict.ContainsKey(sData+"")) continue;
dict.Add(sData+"", "anything"+i);
Debug.WriteLine("Added "+sData + " anything" + i);
}
foreach (string skey in dict.Keys){
//will fail here
Debug.WriteLine(skey + " " + dict[skey+""]);
}
}
public class DokumentSort : IComparer<string>{
Dictionary<string, int> dictPriority = new Dictionary<string, int>();
public DokumentSort(){
//Setup key priority. If unknown key, it should be placed last
string[] sOrder = new string[] { "pri0", "pri1", "pri2" };
for (int i = 0; i < sOrder.Length; i++) {
dictPriority.Add(sOrder+"", i);
}
}
public int Compare(string sX, string sY){
//Debug.WriteLine("Comparing "+sX + " " + sY);
if (sX == sY) return 0;
if(!dictPriority.ContainsKey(sX+"")) return 1;
if(!dictPriority.ContainsKey(sY+"")) return -1;
int priX = dictPriority[sX+""];
int priY = dictPriority[sY+""];
return priX-priY;
}
}