Excel VBA以降序排列数组的最快方法? [英] Excel VBA Quickest way to sort an array of numbers in descending order?

查看:939
本文介绍了Excel VBA以降序排列数组的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按降序排列数组(1000-10000个数,但可能有所不同)的最快方式(按计算时间计算)是什么?据我所知,Excel内置函数的效率并不高,内存中的排序应该比Excel函数快很多。

What is the quickest way (in terms of computational time) to sort an array of numbers (1000-10000 numbers but could vary) in descending order? As far as I know the Excel build-in functions is not really efficient and in-memory sorting should be a lot faster than the Excel functions.

请注意,我不能在电子表格中创建任何东西,所有内容都必须存储在内存中。

Note that I can not create anything on the spreadsheet, everything has to be stored and sorted in memory only.

推荐答案

您可以使用 System.Collections.ArrayList

You could use System.Collections.ArrayList:

Dim arr As Object
Dim cell As Range

Set arr = CreateObject("System.Collections.ArrayList")

' Initialise the ArrayList, for instance by taking values from a range:
For Each cell In Range("A1:F1")
    arr.Add cell.Value
Next

arr.Sort
' Optionally reverse the order
arr.Reverse

这使用快速排序。

这篇关于Excel VBA以降序排列数组的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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