博客
关于我
数组排序系列(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/

你可能感兴趣的文章
vue3+Element-plus icon图标无法显示的问题(已解决)
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>
Nodejs异步回调的处理方法总结
查看>>
NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
查看>>
nodejs支持ssi实现include shtml页面
查看>>
Nodejs教程09:实现一个带接口请求的简单服务器
查看>>
nodejs服务端实现post请求
查看>>
nodejs框架,原理,组件,核心,跟npm和vue的关系
查看>>
Nodejs概览: 思维导图、核心技术、应用场景
查看>>
nodejs模块——fs模块
查看>>
Nodejs模块、自定义模块、CommonJs的概念和使用
查看>>
nodejs生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>
Nodejs连接mysql
查看>>