获取SortList中的最大键

时间:2015-11-30 20:39:34

标签: asp.net vb.net sortedlist

如何确定SortedList中的最大键是什么?我已经尝试使用SortList.Max方法,但我无法使用它。

以下是示例代码:

Dim MySortedList As New SortedList(Of Int16, String)

MySortedList.Add(1, "Item 1")
MySortedList.Add(2, "Item 2")
MySortedList.Add(3, "Item 3")
MySortedList.Add(4, "Item 4")

'I added this just to demonstrates the sorted list is working
Response.Write(MySortedList(4))

'Now... how to I get the max 'Key' from the sorted list?
'In this example I want it to return the number '4'.

Dim MyMaxKey = MySortedList.Max <---- This doesn't work. Added as an example.

Response.Write(MyMaxKey)

2 个答案:

答案 0 :(得分:2)

由于SortedList按键排序,您只需取最后一项的键 - 即最大键:

    Dim MyMaxKey = MySortedList.Last().Key

https://dotnetfiddle.net/l1d3AF

答案 1 :(得分:1)

你可以这样做:

Dim MyMaxKey = MySortedList.Keys.Max()