VBScript script implements array sorting

Source: Internet
Author: User

1, requirements: VBScript the functional support of the array is relatively weak relative to other languages, but in the script application often use arrays, even array sorting, such as: traversing a directory of all the files, and then a rule to save to the array.

2, the implementation method: Since VBScript itself is not functional enough, you can invoke the functionality of the. NET Framework-related components (Mscoree.dll), specifically implemented as follows:

1> Write array sort functions (Parameters: Arrays):

Function asortarray (Array)     dim i,oarraylist, ielement,temparray ()       ' CreateObject (  "System.Collections.ArrayList"  ) that is called Mscoree.dll, is .net  Related components of the framework     set oarraylist = createobject (  " System.Collections.ArrayList " )     For iElement = 0 To  UBound (Array)     oarraylist.add array (ielement)     Next      ' Call object sorting method     oArrayList.Sort    redim  Temparray (Oarraylist.count)     for i=0 to oArrayList.Count-1         if oarraylist.item (i) <> ""  Then             temparray (i)  = oarraylist.item (i)          end if         next    asortarray=temparrayend function  ' Note: Since oarraylist is an object, it does not make sense to do it directly as a return value, so it is necessary to re-process the Oarraylist object and store only its value. The array of the above sort results output is sorted from small to large, to get an array of results from large to small, change temparray (i)  = oarraylist.item (i)   to  temparray ( Arraylist.count-1-i)  = oarraylist.item (i)  .

2> Verification Features:

Sub main Dim arrs,a,i Arrs=array (12,14,70,2,89,412,87,41,8,-7,60) A=asortarray (ARRS) for i=0 to UBound (a) If A (i) <> "then log. Message (A (i)) End If NextEnd Sub


Operation, the output is as follows:

650) this.width=650; "title=" QQ picture 20141103164643.png "Src=" http://s3.51cto.com/wyfs02/M01/4D/B0/ Wkiol1rxqy6yyrkqaaa-zzdwxo8852.jpg "alt=" Wkiol1rxqy6yyrkqaaa-zzdwxo8852.jpg "/>



This article is from the "Qytag (upspringing)" blog, so be sure to keep this source http://qytag.blog.51cto.com/6125308/1571324

VBScript script implements array sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.