13

i have a generic sorted list "results" with key = some filename and value = boolean.

I would like to sort the list by the boolean entry or value column. does anyone know how i can do this?

Thanks!

Grant
  • 11,138
  • 32
  • 94
  • 140

4 Answers4

13

SortedList is optimized so that inertions occur in an ordered fashion, such that enumeration occurs in a sorted order at minimal cost. Anything else requires a re-sort. Thus:

        SortedList<string,bool> l = new SortedList<string, bool>();
        l.Add("a", true);
        l.Add("b", false);
        l.Add("c", true);
        l.Add("d", false);
        var orderByVal = l.OrderBy(kvp => kvp.Value);

but this enumeration will be significantly slower to calculate, and be performed up-front, requiring extra storage to do so.

Depending on your situation it might be cheaper to maintain 2 SortedList instances with the key/value reversed.

spender
  • 117,338
  • 33
  • 229
  • 351
  • 3
    You can't add the same element to the list twice. Throws an exception – Brad Patton Oct 23 '14 at 18:09
  • Don't really see how this is a solution. If I wanted to store a sorted list using their values as keys, which clearly wont even be possible if they're not unique, then I would just store it reversed in the first place. – arkon Jul 03 '23 at 01:05
2

In .NET 2.0, you could add your items to a SortedList:

  public static List<MyObject> SortedObjects(IEnumerable<MyObject> myList) {
     SortedList<string, MyObject> sortedList = new SortedList<string, MyObject>();
     foreach (MyObject object in myList) {
        sortedList.Add(object.ValueIWantToSort, object);
     }

     return new List<MyObject>(sortedList.Values);
  }
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
  • 2
    The problem with that implementation is that the value you want to sort might not be unique as in the OP's question. – Brad Patton Oct 23 '14 at 18:06
2

For descending all list items

list.OrderByDescending(); 

or

var list = list.OrderByDescending(x => x.Product.Name)
                  .ThenBy(x => x.Product.Price).ToList();
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
mansoor
  • 1,553
  • 1
  • 13
  • 18
0

Normally that sorted by the first key on the list so if you swap the key and value on the add, then match that on the binding that sample example i use and work fine

public static SortedList<string, string> GetCountries(string conn)
        {
            var dict = new SortedList<string, string>();
            dict.Add("","Select One");
            var sql = "SELECT [CountryID]      ,[Descr]  FROM [dbo].[Countries] Order By CountryID ";
            using (var rd = GetDataReader(conn, sql))
            {
                while (rd.Read())
                {
                    dict.Add(rd["Descr"].ToString(), rd["CountryID"].ToString());
                }
            }
            return dict;
        }

Dim List As SortedList(Of String, String) = VDB.CoreLib.DbUtils.GetCountries(connDB)

        ddlBankCountry.DataSource = List
        ddlBankCountry.DataTextField = "Key"
        ddlBankCountry.DataValueField = "Value"
        ddlBankCountry.DataBind()
David Fawzy
  • 1,056
  • 15
  • 17