Sorted Hashtable Keys

By default, a hashtable does not keep a sorted list of keys, but sometimes it’s useful to do so.

In order to do this in one line with linq, you’ll need to cast the keys to an array of strings, and then order them as follows:

dailyStatsKeys = dailyStats.Keys.Cast<string>().OrderByx => x).ToList();

or

dailyStatsKeys = dailyStats.Keys.Cast<string>()
.
OrderByDescendingx => x).ToList();

where dailyStats is a hashtable declared and populated elsewhere.

Leave a Reply