1,需求:VBScript对数组的功能支持相对其他语言而言比较弱,然而在脚本应用中经常要使用数组,甚至对数组排序等,例如:遍历某个目录下的所有文件,并按某个规则依次保存到数组中。
2,实现方法:既然VBScript本身的功能不够,那么可以调用。净框架相关组件(mscoree.dll)的功能,具体实现如下:
,,,, 1比;写个数组排序函数(参数:数组):
Function aSortArray(数组) ,,,Dim 我,oArrayList,, iElement, tempArray () ,,,的CreateObject (,“System.Collections.ArrayList”)即是调用了mscoree.dll,是.NET 框架的相关组件 ,,,Set oArrayList =, CreateObject (,“System.Collections.ArrayList”) ,,,For iElement =, 0,用UBound(数组) ,,,oArrayList.Add 数组(iElement) 接下来,,, ,,,的调用对象的排序方法 ,,oArrayList.Sort ,,,redim tempArray (oArrayList.count) ,,,for i=0,用oArrayList.Count-1 ,,,,,,,If oArrayList.item(我)& lt;在“, ,,,,,,,,,,,tempArray (i),=, oArrayList.item(我) ,,,,,,,最终获得If ,,,, 接下来,,, ,,,aSortArray=tempArray 最终获得Function “说明:由于oArrayList是对象,直接做为返回值,意义不大,因此需要对oArrayList对象再处理,只存储它的值就可以了。 的上述排序结果输出的数组是按从小到大排序的,若要得到从大到小的结果数组,将tempArray (i),=, oArrayList.item (i),改为,tempArray (ArrayList.Count-1-i),=, oArrayList.item (i),即可。
,,
,,,, 2在验证功能:
,Sub 主要 ,,,Dim 加勒比海盗,我 ,,,arr=数组(12、14,70,89412,87,41岁,8日,7日,60) ,,,a=aSortArray (arr) ,,,for i=0,用UBound (a) ,,,,,,,If (i) & lt;在“, ,,,,,,,,,,,log.Message (, (i)) ,,,,,,,最终获得If 接下来,,, 最终获得子
运行,输出结果如下: