An alternative approach (I'm not suggesting it's the best one!)
You could have a Dictionary of <KeyValuePair<string, int>, string>. For example, using your data which I've deliberately changed the order on, and just using string for the object for now
var original = new Dictionary<KeyValuePair<string, int>, string>();
original.Add(new KeyValuePair<string, int>("298656", 9), "obj4");
original.Add(new KeyValuePair<string, int>("012395", 0), "obj8");
original.Add(new KeyValuePair<string, int>("892619", 2), "obj12");
original.Add(new KeyValuePair<string, int>("198756", 2), "obj5");
original.Add(new KeyValuePair<string, int>("787362", 8), "obj7");
original.Add(new KeyValuePair<string, int>("158276", 2), "obj3");
Create a custom implementation of IComparer<KeyValuePair<string, int>>
public class MyKVPCompare : IComparer<KeyValuePair<string, int>>
{
public int Compare(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
{
return x.Value.CompareTo(y.Value);
}
}
You can then sort your dictionary using LINQ - here I've done it in priority order then by the "key"
var sorted = original.OrderBy(kvp => kvp.Key, new MyKVPCompare()).ThenBy(a => a.Key.Key);
If I output the results using
foreach (var z in sorted)
Console.WriteLine("Key: {0}, Priority {1}, Value: {2}", z.Key.Key, z.Key.Value, z.Value);
I get
Key: 012395, Priority 0, Value: obj8
Key: 158276, Priority 2, Value: obj3
Key: 198756, Priority 2, Value: obj5
Key: 892619, Priority 2, Value: obj12
Key: 787362, Priority 8, Value: obj7
Key: 298656, Priority 9, Value: obj4
Meant to add that you also have the option of sorting the list by the "key" rather than priority just by using
var sorted2 = original.OrderBy(kvp => kvp.Key.Key);
without changing the source collection to something else.