views:

101

answers:

2

I have the following List :

List<Dictionary<int, Dictionary<string, string>>> lngList
lngList.Add(new Dictionary<int,Dictionary<string,string>>().Add(1,new Dictionary<string,string>().Add("Item1Key","Item1Value")));
lngList.Add(new Dictionary<int,Dictionary<string,string>>().Add(3,new Dictionary<string,string>().Add("Item1Key","Item1Value")));
lngList.Add(new Dictionary<int,Dictionary<string,string>>().Add(2,new Dictionary<string,string>().Add("Item1Key","Item1Value")));
lngList.Add(new Dictionary<int,Dictionary<string,string>>().Add(4,new Dictionary<string,string>().Add("Item1Key","Item1Value")));

I need to sort (ascending) this list on the basis of the integer value present inside the Dictionary.

This has to be achieved without using LINQ.

P.S. This is assuming all the the integer values added are unique.

+3  A: 

If each dictionary has only one key, and you don’t care what happens if it has multiple, you can do this:

lngList.Sort((a, b) => a.Keys.First().CompareTo(b.Keys.First()));

Since you stated that “This has to be achieved without using LINQ”, I assume you mean that the System.Linq namespace is not available to you. But that’s not a problem: you only need .First(), which you can easily define yourself:

public static class EnumerableExtensions {
    public static T First<T>(this IEnumerable<T> source) {
        using (var e = source.GetEnumerator()) {
            if (!e.MoveNext())
                throw new InvalidOperationException("The collection is empty.");
            return e.Current;
        }
    }
}

If you have to use .NET 2.0, which doesn’t have lambda expressions or extension methods, use this instead:

lngList.Sort(new Comparison<Dictionary<int, Dictionary<string, string>>>(sortFunc));

public int sortFunc(Dictionary<int, Dictionary<string, string>> a,
                    Dictionary<int, Dictionary<string, string>> b)
{
    return First(a.Keys).CompareTo(First(b.Keys));
}

public static T First<T>(IEnumerable<T> source) {
    using (var e = source.GetEnumerator()) {
        if (!e.MoveNext())
            throw new InvalidOperationException("The collection is empty.");
        return e.Current;
    }
}
Timwi
Btw, both lambda expressions and extension methods can be used in .NET 2.0. You need to use the C# 3.0 compiler and for extension methods you have to add some metadata to make the compiler happy, but I do this quite frequently myself.
Tergiver
+2  A: 

The easiest way to solve your solution is to use a SortedList instead of a List:

example:

SortedList<int, Dictionary<string, string>> lngList;

this will be sorted by default on the integer value

Wouter Janssens - Xelos bvba
I don’t understand how this answers the question. The question asks about how to sort *a list of dictionaries*, not a list of integer keys. Presumably the asker asked the wrong question?
Timwi
If you look at the code example you will see that he uses a dictionary where it actually is unneeded and I taught he would be better of with a sorted list and I assume it was a good taught because he accepted my answer
Wouter Janssens - Xelos bvba
Right, so the asker asked the wrong question, which is what I thought.
Timwi
maybe it is not a question of the wrong question. If you don't know the Sorted list you start with something else and get stuck an than ask how to get out of it but in many ways it is by starting different :-)
Wouter Janssens - Xelos bvba