afeldspar
afeldspar

Reputation: 1353

Can an ArrayList of objects be sorted by a custom method in VBScript?

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.

Upvotes: 0

Views: 1785

Answers (3)

omegastripes
omegastripes

Reputation: 12612

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.

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

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.

Upvotes: 1

D.Ddgg
D.Ddgg

Reputation: 36

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.

Upvotes: 0

Related Questions