博客
关于我
数组排序系列(8)
阅读量:285 次
发布时间:2019-03-03

本文共 1060 字,大约阅读时间需要 3 分钟。

Sortedlist排序法

'System.Collections.Sortedlist’对象的所有元素自动进行排序。

Sub SortedList()    Dim aintData(1 To 10) As Variant    Dim i As Integer    Dim intLB As Integer    Dim intUB As Integer    Dim avntData(1 To 10) As Variant    Dim objSortedList As Object    intLB = LBound(aintData)    intUB = UBound(aintData)    For i = intLB To intUB        aintData(i) = Application.WorksheetFunction.RandBetween(1, 100)    Next i    Debug.Print "Original Data: " & Join(aintData, ",")    Set objSortedList = CreateObject("System.Collections.Sortedlist")    For i = intLB To intUB        objSortedList.Add aintData(i), aintData(i)    Next i    For i = intLB To intUB        avntData(i) = objSortedList.getkey(i - 1)    Next i    Debug.Print "After Sort: " & Join(avntData, ",")End Sub

运行代码结果如下。

Original Data: 72,8,53,2,38,51,39,63,77,33

After Sort: 2,8,33,38,39,51,53,63,72,77

代码解析:

第14行代码创建’System.Collections.Sortedlist’对象实例。
第15行到第17行代码将数组元素添加的Sortedlist,添加过程中Sortedlist对象自动对所有元素进行排序。


此示例代码实现的是升序排序,如需使用降序,那么只需要修改第19行代码如下。

avntData(i) = objSortedList.getkey(intUB - i)

相关文章链接:

转载地址:http://fbjl.baihongyu.com/

你可能感兴趣的文章
nodejs npm常用命令
查看>>
Nodejs process.nextTick() 使用详解
查看>>
NodeJS yarn 或 npm如何切换淘宝或国外镜像源
查看>>
nodejs 中间件理解
查看>>
nodejs 创建HTTP服务器详解
查看>>
nodejs 发起 GET 请求示例和 POST 请求示例
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 开发websocket 笔记
查看>>
nodejs 的 Buffer 详解
查看>>
NodeJS 的环境变量: 开发环境vs生产环境
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs下的express安装
查看>>
nodejs与javascript中的aes加密
查看>>