return true
return true

Reputation: 250

Sort Dictionary by key then add all values to list

I would like to sort a dictionary by keys then take all the values from that dictionary and put them into an array.

For instance,

Dictionary<int, string> customerNamesByIDs = new Dictionary<int, string>();
customerNamesByIDs.Add(9, "joe");
customerNamesByIDs.Add(2, "bob");

//sort customerNamesByIDs by int
//take sorted dictionary and put them into an array
List<string> customerNames;
//customerNames[0] == "bob";
//customerNames[1] == "joe";

There seems like there should be a simple way to do this, but I have absolutely no idea how. Thanks for your time!!

Upvotes: 1

Views: 335

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

List<string> customerNames = (from c in customerNamesByIDs 
                              orderby c.Key 
                              select c.Value).ToList();

Upvotes: 5

Related Questions