.NET / C# - Convert List to a SortedList

29,288

Solution 1

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.

Solution 2

Do you mean:

  1. you have a List<T> and wish it to be sorted in place?
  2. you have a List<T> and wish to create another 'list' which is itself sorted
  3. you have a List<T> and wish to make a SortedList<T,T> where the key is the same as the value

Assuming input:

var x = new List<int>() { 3, 2, 1 };    

1 is trivial

x.Sort();

2 is trivial

// sx is an IOrderedEnumerable<T>, you can call ToList() on it if you want
var sx = x.OrderBy(i => i); 

3 is trivial with a copy

var s = new SortedList<int,int>(t.ToDictionary(i => i));

and more efficiently:

var s = new SortedList<int,int>();
foreach (var i in x) { s[i] = [i]; }

I can't see why you would want to do 3 but there you go.

Solution 3

Understand that a List<T> is a smart array, and a SortedList<T, U> is a key/value binary tree. Since there's no relationship between their structures, there can't possibly be a more effective way to do it rather than simply taking each element from the list and putting it into the tree.

If you mean "sorted list" instead of "SortedList," then it's trivial to sort your list via either List.Sort() or an appropriate OrderBy().

Solution 4

List unsortedPersons = new List();
// ... Populate unsortedPersons ...
var sorted = from person in unsortedPersons
             orderby person.Name
             select person;

The LINQ gives you an ISortedEnumerable i believe, which may be good enough for your purposes.

Share:
29,288

Related videos on Youtube

BuddyJoe
Author by

BuddyJoe

I like to code C# and work with the web. Still learning.

Updated on July 09, 2022

Comments

  • BuddyJoe
    BuddyJoe almost 2 years

    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.

    • mmx
      mmx almost 15 years
      SortedList is an associative collection, not a simple list. What do you want to set as Key/Value?
    • Eric Lippert
      Eric Lippert almost 15 years
      What's your criterion for "best"? Shortest code, fastest code, most easily understood code, most easily debugged code, most portable code? Something else?
    • BuddyJoe
      BuddyJoe almost 15 years
      I guess most easily understood. But fast/clean/simple/concise would be nice
    • BuddyJoe
      BuddyJoe almost 15 years
      Mehrdad, good question. I think I was going to pull a property from my object as the key. BFree's example I think has me set on the right path.
  • Bijendra Singh
    Bijendra Singh over 14 years
    Rather belatedly - case I have (right or wrong, its a "playground" application) I've retrieved a "list" (more probably IEnumerable) from "elsewhere" that I want to manage in my app as a sorted list - at this point one wants to do a copy - though not, as you point out, with the entry as the key to itself.
  • ShuggyCoUk
    ShuggyCoUk over 14 years
    @Murph cool, do you want the list to be maintained in sorted order (in which case you need a third party class since no such class exists in the framework as 3.5) or are you happy to copy, sort, then never modify it again (in which case you should keep it as an IEnumerable or IOrderedEnumerable)