.NET / C# - Convert List to a SortedList

.net, c#, lambda, list, sortedlist

Solution

var list = new List<string>();
var sortedList = new SortedList<string, string>(list.ToDictionary(s => s));

Now I have no clue how efficient this is, but it's one line of code :) Also, in this example I just used the string itself as the selector. In a real scenario, you should know ahead of time what you'd like to use as a selector.

Problem

What is the best way to convert a List to SortedList? Any good way to do it without cycling through it? Any clever way to do it with an OrderBy()? WRAP UP Please read all answers and comments.

Original source