简体   繁体   English

按升序对SortedList中的值进行排序

[英]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。默认情况下,列表按键按升序排序。我需要按值按升序排序SortedList,而不是按键。

 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: 您可以使用SortedList的通用版本,用于示例:

include the Linq namespace: 包括Linq命名空间:

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 . 您将不会获得新的SortedList,因为默认行为是按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. 正如@Tim评论所示,您可以尝试使用非Cast<> - 通用版本和Cast<>方法将输出转换为IEnumerable<DictionaryEntry>并从那里开始订购。

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." 正如在SortedList文档页面中所写,它“表示按键排序的键/值对的集合,可通过键和索引访问。” 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. 这意味着您必须使用一些更适合您案例的其他数据结构(例如List <KeyValuePair<TKey,TVal>> )并按值对其进行排序。

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): 如果值保证是唯一的,您可以使用两个SortedLists或SortedDictionaries(顺便说一句,如果您不按顺序插入数据,SortedDictionary会更有效,所以我会将它用于第二个集合):

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> ): 如果不保证值是唯一的,那么您可以在需要按值顺序的数据时计算新集合(这里,我假设sortinlist是SortedList<string, string>或其他一些实现IDictionary<string, string>类型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. 如果您仅使用SortedList是因为您希望能够按值对其进行排序,请使用Dictionary<string, string>

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'); ) 添加带前导零的key_value.PadLeft(4, '0'); (例如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. 现在列表已按我的意图排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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