简体   繁体   中英

Sort the value in SortedList by ascending order

I am using SortedList in my application.By default the list is ordered by key in ascending order.I need to order the SortedList by values in ascending order and not by key.

 SortedList sortinlist = new SortedList();

public bool Add(string val1, string val2)
{
       Reading reading = new Reading(val1, val2);
       sortinlist .Add(val1, val2);
       return true;         
}

I goggled with this same topic and refereed some thread,i am not clear in performing this.Can any one help me in doing this.

Thanks

You could use the generic version of the SortedList , for sample:

include the Linq namespace:

using System.Linq;

and try this:

SortedList<string, string> sortinlist = new SortedList<string, string>();

var result = sortinlist.OrderBy(x => x.Value).ToList();

You will not get a new SortedList because the default behaviour is ordered by Key .

As @Tim comment bellow, you could try with the non-generic version with the Cast<> method to convert the ouput to a IEnumerable<DictionaryEntry> and order from there.

var result = sortinlist.Cast<DictionaryEntry>().OrderBy(x => x.Value);

Maybe you should reconsider changing the data and data structure you are using. As written in the documentation page for the SortedList , it "Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index." This means that you have to use some other data structure that is more appropriate for your case (a List <KeyValuePair<TKey,TVal>> for example) and sort it by value.

If the values are guaranteed to be unique, you can use two SortedLists or SortedDictionaries (by the way, SortedDictionary is much more efficient if you are inserting data out of order, so I'll use that for the second collection):

SortedList sortinlist = new SortedList();
SortedDictionary valueOrder = new SortedDictionary<string, string>();

public bool Add(string val1, string val2)
{
       Reading reading = new Reading(val1, val2);
       sortinlist.Add(val1, val2);
       valueOrder.Add(val2, val1);
       return true;
}

If the values are not guaranteed to be unique, then you can either calculate a new collection whenever you need the data in value order (here, I assume that sortinlist is a SortedList<string, string> or some other type that implements IDictionary<string, string> ):

var valueOrder = sortinlist.OrderBy(kvp => kvp.Value).ToList();

... or maintain a separate collection:

SortedList<string, string> sortinlist = new SortedList<string, string>();
List<KeyValuePair<string, string>> valueOrder = new List<KeyValuePair<string, string>>();

public bool Add(string val1, string val2)
{
       Reading reading = new Reading(val1, val2);
       sortinlist.Add(val1, val2);
       int targetIndex = /* to do: calculate proper index */
       valueOrder.Insert(targetIndex, new KeyValuePair<string, string>(val2, val1));
       return true;
}

If you are using the SortedList only because you were hoping to be able to sort it by values, use Dictionary<string, string> instead.

This is what I did in order to sort the list per the required order.

  1. Sort the table from the query
  2. Add lpad with leading zeros ( eg key_value.PadLeft(4, '0'); )
  3. Add the Key Value pair to the sorted list
  4. Now the list is already sorted as I intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM