A Dictionary<TKey, TValue> is not kept in sorted order, and it has no defined order. This is because it is implemented using a hashing algorithm.
Although there is a SortedDictionary available, it doesn't look suitable for what you want.
Instead it looks like you should use a list of dictionaries, and use a custom comparer to sort it:
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
using StrStrDict = Dictionary<string, string>;
public sealed class CustomComparer: IComparer<StrStrDict>
{
public int Compare(StrStrDict lhs, StrStrDict rhs)
{
double x = Double.Parse(lhs["number"]);
double y = Double.Parse(rhs["number"]);
return x.CompareTo(y);
}
}
internal class Program
{
private static void Main()
{
var dict1 = new StrStrDict
{
{"name", "name1"},
{"number", "0.0158"}
};
var dict2 = new StrStrDict
{
{"name", "name2"},
{"number", "0.0038"}
};
var dict3 = new StrStrDict
{
{"name", "name3"},
{"number", "0.0148"}
};
var list = new List<StrStrDict> {dict1, dict2, dict3};
list.Sort(new CustomComparer());
foreach (var element in list)
{
Console.WriteLine("Number = " + element["number"]);
}
}
}
}
Note that this isn't quite like your example, because you seem to be storing actual doubles rather than string representations of doubles, but that doesn't match with your use of Dictionary<string, string>. Therefore I have stored them as strings and parse them into doubles on demand. That isn't very efficient, and the alternative is to use a Dictionary<string, object> and cast the values in the comparer - see the example later.
Also note that I haven't added any error handling at all!
Here's an example using a Dictionary<string, object> instead:
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
using StrObjDict = Dictionary<string, object>;
public sealed class CustomComparer: IComparer<StrObjDict>
{
public int Compare(StrObjDict lhs, StrObjDict rhs)
{
double x = (double)lhs["number"];
double y = (double)rhs["number"];
return x.CompareTo(y);
}
}
internal class Program
{
private static void Main()
{
var dict1 = new StrObjDict
{
{"name", "name1"},
{"number", 0.0158}
};
var dict2 = new StrObjDict
{
{"name", "name2"},
{"number", 0.0038}
};
var dict3 = new StrObjDict
{
{"name", "name3"},
{"number", 0.0148}
};
var list = new List<StrObjDict> {dict1, dict2, dict3};
list.Sort(new CustomComparer());
foreach (var element in list)
{
Console.WriteLine("Name = {0}, Number = {1}", element["name"], element["number"]);
}
}
}
}
Finally, note that this approach only makes sense if you want to dynamically store other things in the dictionaries. If you are only ever storing "name" and "number" in each dictionary then you should just create a specific class to store that data rather than using a dictionary!
That approach might look like this - note how much simpler it is (and typesafe too!):
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
public sealed class Entry
{
public readonly string Name;
public readonly double Number;
public Entry(string name, double number)
{
Name = name;
Number = number;
}
}
internal class Program
{
private static void Main()
{
var list = new List<Entry>
{
new Entry("name1", 0.0158),
new Entry("name2", 0.0038),
new Entry("name3", 0.0148)
};
list.Sort((lhs, rhs) => lhs.Number.CompareTo(rhs.Number));
// Alternatively if you don't want an in-place sort and you
// want to keep the original unsorted list, you can create
// a separate sorted list using Linq like so:
//
// var sortedList = list.OrderBy(x => x.Number).ToList();
foreach (var element in list)
{
Console.WriteLine("Name = {0}, Number = {1}", element.Name, element.Number);
}
}
}
}
myDictactually defined as? It appears that it might be a collection of dictionaries?