25287

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

Question:

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.

Answer1:

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.

Answer2:

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.

Answer3:

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.

Recommend

  • All is on UTF8 but I still got strange char like �
  • processing files in 64 bit machine but developing in 32 bit machine
  • LINQ Query Help
  • implement alphabetical scroll in MPMediaPickerController app
  • Add a favicon to redmine theme
  • Insert row into table alphabetically on a certain cell
  • Reverse an array upside down?
  • Nunit Tests order of Execution
  • Joomla How to customize main menu
  • qticon only showing on my pc
  • Undefined reference when using a function included in a header file [duplicate]
  • Best way to handle offline and online development with Git
  • D3 Tree layout visualization - Inherit child with multiple parents
  • Override Swagger sorting
  • How to determine mime type by the file extension? ( Ruby )
  • On which thread JavaFX change listeners are executed ?
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Why are “sc.addFile” and “spark-submit --files” not distributing a local file to all workers?
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Why can't UI components be accessed from a backgroundworker?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Can't remove headers after they are sent
  • Firefox Extension - Monitor refresh and change of tab
  • Alternative to overridePendingTransition() - Android
  • How can I sort a a table with VBA with given text condition?
  • Is it possible to access block's scope in method?
  • C# - Serializing and deserializing static member
  • Cannot Parse HTML Data Using Android / JSOUP
  • Acquiring multiple attributes from .xml file in c#
  • Java static initializers and reflection
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • Does armcc optimizes non-volatile variables with -O0?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app