Sorting a Dictionary

  • Thread starter Thread starter eBob.com
  • Start date Start date
E

eBob.com

I've just learned about Dictionaries and I like them. But I need to sort
one, in key order, and I can't find a way to do that. Well, I could
certainly write the code, but it just seems like there should be some method
to do it but I can't find one. Strangely, IntelliSense seems to know about
a Dictionary.Orderby method but I can't find any doc on OrderBy (unless it's
the LINQ OrderBy, the doc of which looks a bit over my head and lacks an
example - at least what I have found so far; and I haven't begun to look
into LINQ yet).

So ... any way to sort a Dictionary in key order without having to write a
sort routine?

Thanks, Bob
 
I've just learned about Dictionaries and I like them. But I need to sort
one, in key order, and I can't find a way to do that. Well, I could
certainly write the code, but it just seems like there should be some method
to do it but I can't find one. Strangely, IntelliSense seems to know about
a Dictionary.Orderby method but I can't find any doc on OrderBy (unless it's
the LINQ OrderBy, the doc of which looks a bit over my head and lacks an
example - at least what I have found so far; and I haven't begun to look
into LINQ yet).

So ... any way to sort a Dictionary in key order without having to write a
sort routine?

Thanks, Bob

System.Collections.Generic.SortedDictionary
 
use a SortedDictionary?
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.collections.generic/html/55ea011a-bd55-49ed-2376-559c3fd965ed.htm


Represents a collection of key/value pairs that are sorted on the key.

Namespace: System.Collections.Generic
Assembly: System (in System.dll)

hth

mark
 
eBob.com said:
I've just learned about Dictionaries and I like them. But I need to sort
one, in key order, and I can't find a way to do that. Well, I could
certainly write the code, but it just seems like there should be some
method to do it but I can't find one. Strangely, IntelliSense seems to
know about a Dictionary.Orderby method but I can't find any doc on OrderBy
(unless it's the LINQ OrderBy, the doc of which looks a bit over my head
and lacks an example - at least what I have found so far; and I haven't
begun to look into LINQ yet).

So ... any way to sort a Dictionary in key order without having to write a
sort routine?

The example happens to be in C#, but you can do it too in VB by using Linq.
You should be able to use a online C#-2-VB.NET code converter to see it in
VB or find a VB example by using Bing or Google.

http://dotnetperls.com/sort-dictionary-values



What is Language Integrated Query?

LINQ is a Microsoft .NET Framework component that adds native data querying
capabilities to .NET languages.

Microsoft LINQ defines a set of query operators that can be used to query,
project and filter data in arrays, enumerable classes, XML, relational
database, and third party data sources. While it allows any data source to
be queried, it requires that the data be encapsulated as objects. So, if the
data source does not natively store data as objects, the data must be mapped
to the object domain. Queries written using the query operators are executed
either by the LINQ query processing engine or, via an extension mechanism,
handed over to LINQ providers which either implement a separate query
processing engine or translate to a different format to be executed on a
separate data store (such as on a database server as SQL queries). The
results of a query are returned as a collection of in-memory objects that
can be enumerated using a standard iterator function such as C#'s foreach.

Many of the concepts that LINQ has introduced were originally tested in
Microsoft's C? research project. LINQ was released as a part of .NET
Framework 3.5 on November 19, 2007.

http://en.wikipedia.org/wiki/Language_Integrated_Query


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4516 (20091016) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
eBob.com said:
I've just learned about Dictionaries and I like them. But I need to
sort one, in key order, and I can't find a way to do that. Well, I
could certainly write the code, but it just seems like there should be
some method to do it but I can't find one. Strangely, IntelliSense
seems to know about a Dictionary.Orderby method but I can't find any doc
on OrderBy (unless it's the LINQ OrderBy, the doc of which looks a bit
over my head and lacks an example - at least what I have found so far;
and I haven't begun to look into LINQ yet).

So ... any way to sort a Dictionary in key order without having to write
a sort routine?

Thanks, Bob

In addition to the other responses, if you just occasionally need the
keys in sorted order, then you can do this:

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Red", "Communists");
d.Add("Green", "Al Gore");
d.Add("Blue", "A sad clown");
List<string> keys = d.Keys.ToList<string>();
keys.Sort();
foreach (string k in keys) Console.WriteLine(k);
Console.ReadKey();

One should rarely, if ever these days, write their own sorting routines.
 
Family said:
In addition to the other responses, if you just occasionally need the
keys in sorted order, then you can do this:

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Red", "Communists");
d.Add("Green", "Al Gore");
d.Add("Blue", "A sad clown");
List<string> keys = d.Keys.ToList<string>();
keys.Sort();
foreach (string k in keys) Console.WriteLine(k);
Console.ReadKey();

One should rarely, if ever these days, write their own sorting routines.

Sorry, forgot which group I was in, but hopefully you get the idea. :)
 
Thanks to all responders for the helpful info. I went with
SortedDictionary. Great solution for me as I have a very small dictionary.

Thanks, Bob
 
Back
Top