Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

VBScript script implements array sorting

Shulou Source: shulou.com Published: 2022-06-03 06:21:52 09月21日 Update

1, requirements: VBScript on the array function support is relatively weak compared to other languages, however, in the script to often use the array, and even array sorting, such as: traversing a directory of all files, and according to a certain rule saved to the array in turn.

2. Implementation method: since the function of VBScript itself is not enough, you can call the function of .NET Framework-related components (mscoree.dll) as follows:

1 > write an array sort function (parameter: array):

Function aSortArray (Array) Dim I CreateObject ArrayList, iElement,tempArray () 'ArrayList ("System.Collections.ArrayList") is called mscoree.dll. Is the related component of .NET Framework Set oArrayList = CreateObject ("System.Collections.ArrayList") For iElement = 0 To UBound (Array) oArrayList.Add Array (iElement) Next 'call the object's sorting method oArrayList.Sort redim tempArray (oArrayList.count) for item0 to oArrayList.Count-1 If oArrayList.item (I) "Then tempArray (I) = oArrayList.item (I) End If Next aSortArray=tempArrayEnd Function 'description: because oArrayList is an object It doesn't make much sense to use it directly as a return value, so you need to reprocess the oArrayList object and just store its value.' The array of the output of the above sorting results is sorted from small to large. To get the array of results from large to small, change tempArray (I) = oArrayList.item (I) to tempArray (ArrayList.Count-1-i) = oArrayList.item (I).

2 > Verification function:

Sub main Dim arrs,a,i arrs=Array (arrs) for item0 to UBound (a) If a (I) "Then log.Message (a (I))"

Run, and the output is as follows:

Tags: Array sort function object result method component output script from small to large not enough not large number small function parameter meaning file directory rule Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple MySQL vpn Huawei