# 利用集合进行数组的排序

笔者在学习VB中发现利用集合可以进行排序，优化后比冒泡法快得多。下面是完整的函数代码，演示了如何进行数组的升序或降序排列。

Option Explicit
Dim X(100) As Double
Dim y(100) As String
Private Sub Command1_Click() '演示数字排序
Dim a(100) As Double, z(100) As String, i As Long
For i = 0 To 100
a(i) = X(i) '复制数组
z(i) = CStr(X(i)) '转化为字符串数组
Next
MsgBox Join(z, ","), 64, "原始数组" '显示原始顺序

NUMBERSORT X, "up"
For i = 0 To 100
z(i) = CStr(X(i)) '转化为字符串数组
Next
MsgBox Join(z, ","), 64, "按数字升序排序后数组" '显示排序结果

NUMBERSORT a, "down"
For i = 0 To 100
z(i) = CStr(a(i)) '转化为字符串数组
Next
MsgBox Join(z, ","), 64, "按数字降序排序后数组" '显示排序结果

End Sub
Sub NUMBERSORT(ByRef a() As Double, Optional sort As String = "up") '数字排序
Dim min As Long, max As Long, num As Long, first As Long, last As Long, temp As Long, all As New Collection, steps As Long
min = LBound(a)
max = UBound(a)
steps = 1
For num = min + 1 To max
last = all.Count
If a(num) < CDbl(all(1)) Then all.Add a(num), BEFORE:=1: GoTo nextnum '加到第一项
If a(num) > CDbl(all(last)) Then all.Add a(num), AFTER:=last: GoTo nextnum '加到最后一项

first = 1
Do While last > first + 1 '利用DO循环减少循环次数
temp = (last + first) / 2
If a(num) > CDbl(all(temp)) Then
first = temp
Else
last = temp
steps = steps + 1
End If
Loop

nextnum:
steps = steps + 1
Next
For num = min To max
If sort = "UP" Or sort = "up" Then a(num) = CDbl(all(num - min + 1)): steps = steps + 1 '升序
If sort = "DOWN" Or sort = "down" Then a(num) = CDbl(all(max - num + 1)): steps = steps + 1 '降序
Next
MsgBox "本数组共经过 " & steps & "步实现" & IIf(sort = "UP" Or sort = "up", "升序", "降序") & "排序！", 64, "INFORMATION"
Set all = Nothing
End Sub

Sub STRINGSORT(ByRef a() As String, Optional sort As String = "UP") '字符串排序
Dim min As Long, max As Long, num As Long, first As Long, last As Long, temp As Long, all As New Collection, steps As Long
min = LBound(a)
max = UBound(a)
steps = 1
For num = min + 1 To max

first = 1
last = all.Count
If a(num) < all(1) Then all.Add a(num), BEFORE:=1: GoTo nextnum '加到第一项
If a(num) > all(last) Then all.Add a(num), AFTER:=last: GoTo nextnum '加到最后一项

Do While last > first + 1 '利用DO循环减少循环次数
temp = (last + first) / 2
If a(num) > all(temp) Then
first = temp
Else
last = temp
steps = steps + 1
End If
Loop

nextnum:
steps = steps + 1
Next
For num = min To max
If sort = "UP" Or sort = "up" Then a(num) = all(num - min + 1): steps = steps + 1 '升序
If sort = "DOWN" Or sort = "down" Then a(num) = all(max - num + 1): steps = steps + 1 '降序
Next
MsgBox "本数组共经过 " & steps & "步实现" & IIf(sort = "UP" Or sort = "up", "升序", "降序") & "排序！", 64, "INFORMATION"
Set all = Nothing
End Sub

Private Sub Command2_Click() '演示字符串排序
Dim z(100) As String, i As Long '复制数组
For i = 0 To 100
z(i) = y(i)
Next

MsgBox Join(y, ","), 64, "原始数组" '显示原始顺序

STRINGSORT y, "UP"
MsgBox Join(y, ","), 64, "按字符串升序排序后数组" '显示排序结果

STRINGSORT z, "DOWN"
MsgBox Join(z, ","), 64, "按字符串降序排序后数组" '显示排序结果
End Sub

Private Sub Command3_Click() ' 排序计时
Dim a(3000) As String, i As Long, starttime As Long, endtime As Long
For i = 0 To 3000
a(i) = Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) '生成随机6字符的字符串
Next
starttime = Timer '计时开始
STRINGSORT a
endtime = Timer '计时结束
MsgBox "排序共耗时 " & endtime - starttime & " 秒！"
End Sub

Randomize
Dim i As Long
For i = 0 To 100
X(i) = Format(Rnd * 1000, "0.00")
y(i) = Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65)
Next
End Sub

• 本文已收录于以下专栏：

## 对List集合中的元素进行排序

Collections对List集合中的数据进行排序 有时候需要对集合中的元素按照一定的规则进行排序，这就需要用到 Java中提供的对集合进行操作的工具类Collections，其中的so...
• veryisjava
• 2016年06月14日 22:08
• 108181

## 使用冒泡对数组进行排序

• 2016年07月08日 08:09
• 695

## 数组的排序及查找

1.选择排序：每一轮选择该轮第一个数和后面的每一个数进行比较，满足条件交换位置。                     for(int i=0;i                     {    ...
• linbin23023
• 2013年08月23日 22:18
• 413

## 数组中元素的排序，查找

• LHS19940203
• 2017年05月08日 22:38
• 191

## java中如何对数组和集合进行排序

package com.hsy.interView; import java.util.ArrayList; import java.util.Collections; import java....
• hu_shengyang
• 2011年03月14日 21:18
• 6857

## 对整形数组进行快速排序

• A_stupid_dog
• 2016年06月23日 16:18
• 555

## jS中 用for循环进行对数组中数字升降排序

• caomingwumr8
• 2017年10月17日 16:47
• 150

## 用指针对数组进行排序

• u012491514
• 2014年03月26日 10:24
• 2098

## java集合和数组的排序总结

• DanEmpress
• 2016年09月12日 19:45
• 752

## List集合、Map集合、数组的排序

1、对File数组根据修改时间进行排序 Arrays.sort(Files, new Comparator() { @Override public int compare(File f1, Fi...
• u014482758
• 2015年07月14日 20:13
• 837

举报原因： 您举报文章：利用集合进行数组的排序 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)