查看: 582|回复: 2

[文章教程] 借助.NET大哥的SortedList类,创建排序集合。

[复制链接]
xuanxiao 发表于 2022-5-8 10:30:03 | 显示全部楼层 |阅读模式
ASP应用程序中使用System.Collections.Sortedlist 可按照键和索引访问,自动排序。
SortedList 类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问。

主要成员:
/* 属性 */
Capacity;        //容量
Count;           //元素数
Keys;            //键集合(ICollection)
Values;          //值集合(ICollection)

/* 方法 */
Add()           //添加
Clear()         //清空
Contains()      //是否包含指定键
ContainsKey()   //同 Contains()
ContainsValue() //是否包含指定值
GetByIndex()    //根据索引取 Value
GetKey()        //根据索引获取 Key
GetKeyList()    //取键列表(IList)
GetValueList()  //取值列表(IList)
IndexOfKey()    //获取指定键的索引
IndexOfValue()  //获取指定值的索引
Remove()        //根据键值删除
RemoveAt()      //根据索引删除
SetByIndex()    //根据索引设置值
TrimToSize()    //优化容量(Capacity = Count)

  1. <%
  2. response.Charset="utf-8"
  3. set list = server.createObject("System.Collections.Sortedlist")
  4. with list        
  5.         .add "Second", "是"
  6.         .add "Third", "美丽的日子"
  7.         .add "First", "今天"
  8. end with

  9. for i = 0 to list.count - 1
  10.   response.write(list.getKey(i) & " = " & list.getByIndex(i))&"<br>"
  11. next
  12. set list = nothing
  13. %>
复制代码

QQ截图20220508102941.png

  1. response.Charset="utf-8"
  2. Dim objDict
  3. Set objDict = Server.CreateObject("System.Collections.Sortedlist")
  4.         objDict.Add "张三", "80分"
  5.         objDict.Add "李四", "60分"
  6.         objDict.Add "王五", "90分"
  7.         objDict.Add "赵六", "20分"
  8. response.Write "王五分数是:" & vbTab & objDict.GetByIndex(objDict.IndexOfKey("王五"))
  9. Set objDict = Nothing
复制代码


1.png

  1. response.Charset="utf-8"
  2. Dim objDict
  3. Dim objSortedList
  4.     Dim objList2
  5.     Dim I

  6.     Set objSortedList = Server.CreateObject("System.Collections.Sortedlist")

  7.     objSortedList.Add "First", "AAAA"
  8.     objSortedList.Add "Second", "!"
  9.     objSortedList.Add "Third", "CCCC"
  10.     objSortedList.Add "Fourth", ","

  11.     Response.Write objSortedList.IndexOfKey("First")&vbnewline
  12.     Response.Write objSortedList.IndexOfValue("AAAA")&vbnewline
  13.     Response.Write objSortedList.IndexOfKey("Second")&vbnewline
  14.     Response.Write objSortedList.IndexOfValue("!")&vbnewline
  15.     Response.Write objSortedList.IndexOfKey("Third")&vbnewline
  16.     Response.Write objSortedList.IndexOfValue("CCCC")&vbnewline
  17.     Response.Write objSortedList.IndexOfKey("Fourth")&vbnewline
  18.     Response.Write objSortedList.IndexOfValue(",")&vbnewline


  19.     For I = 0 To objSortedList.Count - 1
  20.         Response.Write objSortedList.GetKey(I) & vbTab & objSortedList.GetByIndex(I)&vbnewline
  21.     Next
  22.     Response.Write "Size     : " & objSortedList.Count&vbnewline
  23.     Response.Write "Capacity : " & objSortedList.Capacity&vbnewline

  24.     Response.Write objSortedList.GetByIndex(objSortedList.IndexOfKey("Third"))&vbnewline

  25.     objSortedList.TrimToSize

  26.     Response.Write "Size     : " & objSortedList.Count&vbnewline
  27.     Response.Write "Capacity : " & objSortedList.Capacity&vbnewline

  28.     Set objList2 = objSortedList.Clone
  29.     Response.Write "Sorted List Key(1) = " & objSortedList.GetKey(1)&vbnewline
  30.     Response.Write "Cloned List Key(1) = " & objList2.GetKey(1)

  31.     Set objList2 = Nothing
  32.     Set objSortedList = Nothing
复制代码
大个子 发表于 2022-5-10 13:47:10 | 显示全部楼层
本帖最后由 大个子 于 2022-5-16 16:40 编辑

aaaaaaaaaaaaa
大个子 发表于 2022-5-10 13:47:36 | 显示全部楼层
本帖最后由 大个子 于 2022-5-16 16:40 编辑

bbbbbbbbbbbb
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号