按值排序列表 [英] Sorted List by value

查看:70
本文介绍了按值排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我的Key-Value SortedList有问题。


有没有办法对列表进行排序(或任何其他数据结构)按值计算?


我有一个包含1000个键值对的列表,并希望得到

这对具有第20个最高值的对价值。

问候,

Martin

Hello,

I?′ve a problem with my Key-Value SortedList.

Is there a way to sort a list (or any other data structure) by value?

I have a list with 1000 key-value pairs and want to get
the pairs with the 20st highest values.
Regards,
Martin

推荐答案

SortedList总是按值排序 - 但是,你或许可以

创建一个List< T from .Values,和.Sort(),使用一个

鹅卵石比较器/委托,否则只需在默认情况下反转sort:

//虚拟数据

SortedList< int,intsl = new SortedList< int,int>();

sl.Add( 1,6);

sl.Add(2,1);

sl.Add(3,3);

sl.Add (100,2);

sl.Add(4,17);

//排序值降序

列表< intl =新列表< ; int>(sl.Values);

l.Sort();

l.Reverse();

//输出(int i = 0;最高3

;我< 3; i ++)

{

Console.WriteLine(l [i]);

}


Marc
SortedList is always sorted by value - however, you could perhaps
create a List<Tfrom the .Values, and .Sort() that, using either a
cobbled comparer / delegate, else just reverse after the default sort:
// dummy data
SortedList<int, intsl = new SortedList<int, int>();
sl.Add(1, 6);
sl.Add(2, 1);
sl.Add(3, 3);
sl.Add(100, 2);
sl.Add(4, 17);
// sort values descending
List<intl = new List<int>(sl.Values);
l.Sort();
l.Reverse();
// output the highest 3
for (int i = 0; i < 3; i++)
{
Console.WriteLine(l[i]);
}

Marc





" Martin P?pping" < ma ****** @ despammed.com写信息

新闻:eq ********** @ newsreader3.netcologne.de ...

|您好,

|

|我的Key-Value SortedList出了问题。

|

|有没有办法按值对列表(或任何其他数据结构)进行排序?


正如您可能发现的那样,它仅由Key订购,而不是按值排序。 />
这就是说我没有看到干净或聪明的解决方案。你可以随时

创建一个倒映射的新实例(原始键是值)。

-

Ignacio Machin

machin AT laceupsolutions com
Hi,

"Martin P?pping" <ma******@despammed.comwrote in message
news:eq**********@newsreader3.netcologne.de...
| Hello,
|
| I′ve a problem with my Key-Value SortedList.
|
| Is there a way to sort a list (or any other data structure) by value?

As you probably found out it''s ordered only by Key , not by value.
That said I do not see a clean or clever solution for this. You could always
create a new instance with inverted mapping ( original keys are values).
--
Ignacio Machin
machin AT laceupsolutions com


Marc Gravell schrieb:
Marc Gravell schrieb:

SortedList总是按值排序 - 但是,你或许可以

创建一个List< T from .Values,和.Sort(),使用一个

鹅卵石比较器/委托,否则只需在默认情况下反转sort:

//虚拟数据

SortedList< int,intsl = new SortedList< int,int>();

sl.Add( 1,6);

sl.Add(2,1);

sl.Add(3,3);

sl.Add (100,2);

sl.Add(4,17);

//排序值降序

列表< intl =新列表< ; int>(sl.Values);

l.Sort();

l.Reverse();

//输出最高3

for(int i = 0;我< 3; i ++)

{

Console.WriteLine(l [i]);

}
SortedList is always sorted by value - however, you could perhaps
create a List<Tfrom the .Values, and .Sort() that, using either a
cobbled comparer / delegate, else just reverse after the default sort:
// dummy data
SortedList<int, intsl = new SortedList<int, int>();
sl.Add(1, 6);
sl.Add(2, 1);
sl.Add(3, 3);
sl.Add(100, 2);
sl.Add(4, 17);
// sort values descending
List<intl = new List<int>(sl.Values);
l.Sort();
l.Reverse();
// output the highest 3
for (int i = 0; i < 3; i++)
{
Console.WriteLine(l[i]);
}



非常感谢!

但是这不会有帮助。


因为我需要3个最高值的3个键;)


使用您的解决方案我只能获得没有键的3个最高值。

在我看来,没有真正的解决方案,

除了交换键和值。

问候,

马丁

Thanks a lot!
But this won?′t help.

Because I need the 3 keys of the 3-highest values ;)

With your solution I only get the 3-highest values without keys.
It seems to me that there is no real solution,
except swapping keys- and values.
Regards,
Martin


这篇关于按值排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆