0

First off - the language I'm using is VBScript, used with Windows Scripting Host. I don't really have the option of changing that and using something else.

At this point, I have a number of records with multiple fields, read in from a file. I want to print them out in a number of orderings (first, in alphabetical order; second, grouped by location and then sorted alphabetically within locations; etc.). The records are stored in an ArrayList.

If I was using Visual Basic rather than VBScript, I know that I could create classes which implemented the iComparer interface, and then call the Sort function of the ArrayList with an object of the appropriate class when I wanted to sort in that order (use the Alphabetical object for the first sort, the LocationThenAlphabetical object for the second sort, etc.)

However, I have been going crazy trying to find a way to do that or anything similar in VBScript, the language I actually have available to work with. I've Googled endless combinations of "VBScript", "ArrayList", "sort", "custom sort", "comparison", "custom comparison", "implements", "interface" - but whenever I find results relevant for VBScript, they aren't about how to do a custom sort, and whenever I find results relevant to custom sorting, they aren't applicable to VBScript, even if that was clearly specified in my search.

I would rather do this the right way, if that's possible. If it's simply not possible to do a custom sort in VBScript, I can see a possible work-around, but it would add a lot of complexity and use a lot more memory than I think a custom sort would do.

3 Answers 3

1

Given that you'd have to pass a System.Collections.IComparer to the .Sort method and that you can't create .NET objects in VBScript, the answer is no.

Sign up to request clarification or add additional context in comments.

1 Comment

VBScript can create classes and use objects from .Net, so surely there must be a way. If the "List" is not inherent to VBScript and is created by means of: dim list: Set list = CreateObject("System.Collections.ArrayList") But maybe it's because of other functionality lacking in VBScript - like the ability to use types and implement interfaces? (required by this VB.Net example: VB.net Sort Arraylist By Objects Name)
1

Regarding work-around. You can make custom sort easily with JScript. Even being limited to WSH VBScript, you can instantiate MSScriptControl.ScriptControl, set it's language to JScript, create stuff that would be able to receive all the necessary data from file via VBS to the JS array, and then to sort with arrayObj.sort( [sortFunction] ) using function sortFunction(a, b){} as comparer.

Comments

0
Set rs = CreateObject("ADODB.Recordset")
With rs
    .Fields.Append "SortKey", 201, 260 
    .Fields.Append "Txt", 201, 5000 
    .Open
    Do Until Inp.AtEndOfStream
        Lne = Inp.readline
        SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3))
        .AddNew
        .Fields("SortKey").value = SortKey
        .Fields("Txt").value = Lne
        .UpDate
    Loop
    If Arg(2) = "a" then SortColumn = "SortKey ASC"
    If Arg(2) = "d" then SortColumn = "SortKey DESC"
    .Sort = SortColumn
    Do While not .EOF
        Outp.writeline .Fields("Txt").Value
        .MoveNext
    Loop
End With

This is sorting a file using a disconnected recordset.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.