2

I am a C student in VBScript coding and need a little help.

My code executes a split command as follows:

outputArray = split(Description," ")

With the individual words from Description now in an array, I want to sort the array based on the string length for each word.

So, for example, if Description is equal to "this is an example of a description" then my array values are [this, is, an, example, of, a, description], right?

But I want to resort the array so that the longest words are first, i.e. the array items are ordered by string length. So, after some VBScript code that I can't seem to figure out, the array would look like this: [description, example, this, an, is, of, a]

If there's a tie for string length, the secondary sort would be alphabetical.

I would greatly appreciate some help on this from an A student out there. Thanks.

user1883779
  • 61
  • 1
  • 8

2 Answers2

4

As VBScript has no native sort, it needs a little help from a friend. In your case - because of your more complex sorting criteria - the friend should not be .Net's ArrayList, JScript's sort, or sort.exe (introduced here), but a disconnected ADO recordset:

  Const adInteger          =          3 ' 00000003
  Const adVarChar          =        200 ' 000000C8

  Dim sInp : sInp = "this is an example of a description"
  Dim aInp : aInp = Split(sInp)
  WScript.Echo "A:", Join(aInp)

  Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
  oRS.Fields.Append "Word", adVarChar, 50
  oRS.Fields.Append "Length", adInteger
  oRS.Open
  Dim sWord
  For Each sWord In aInp
      oRS.AddNew
      oRS.Fields("Word").value = sWord
      oRS.Fields("Length").value = Len(sWord)
      oRS.UpDate
  Next
  oRS.Sort = "Length DESC, Word"

  Dim aTable : aTable = oRS.GetRows()
  ReDim aOut(UBound(aTable, 2))
  Dim i
  For i = 0 To UBound(aOut)
      aOut(i) = aTable(0, i)
  Next
  WScript.Echo "B:", Join(aOut)

output:

A: this is an example of a description
B: description example this an is of a

For background start here.

ADDED - For ArrayList Aficionados:

A Disconnected Recordset should be your first choice, if your data is in essence tabular (sort criteria involves more than one aspect/property of the elements).

ArrayList sorting in VBScript is good for simple cases only, because - AFAIK - you can't pass a compare function to the sort method. Please, prove me wrong!

If you must use an ArrayList for more complex sorting, consider the Schwartzian transform:

  1. prepare customized temporary data to ease comparisons
  2. sort
  3. recover original data

In code:

  Const csSep = "|"
  Const cnMax = 100

  Dim sInp : sInp = "this is an example of a description"
  Dim aInp : aInp = Split(sInp)
  WScript.Echo "A:", Join(aInp)

  Dim oNAL : Set oNAL = CreateObject( "System.Collections.ArrayList" )
  Dim oSB  : Set oSB  = CreateObject( "System.Text.StringBuilder" )
  Dim sWord
  For Each sWord In aInp
      oSB.AppendFormat_3 "{0,4}{1}{2}", 100 - Len(sWord), csSep, sWord
      sWord = oSB.ToString()
      oSB.Length = 0
      oNAL.Add sWord
  Next
  oNAL.Sort

  ReDim aOut(oNAL.Count - 1)
  Dim i
  For i = 0 To UBound(aOut)
      aOut(i) = Split(oNAL(i), csSep)(1)
  Next
  WScript.Echo "B:", Join(aOut)

output:

A: this is an example of a description
B: description example this an is of a
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • I like the ADODB recordset approach. I would have done it by using an array of arraylists, where the index of the array keeps the length and the arraylist the words with that length. Then sort each arraylist to get the words in alphabetical order. – AutomatedChaos Dec 07 '12 at 12:15
  • Thanks, Ekkehard. I am going to try this out later today. Really appreciate the help. – user1883779 Dec 07 '12 at 18:25
  • To paraphrase Wayne and Garth in the movie Wayne's World: "I am not worthy! I am not worthy!" Thank you. This code worked perfectly for me. Really appreciate your help. – user1883779 Dec 07 '12 at 19:32
0

here is a helpful link on how to sort by length and in alphabetical order

http://www.webknowhow.net/dir/ASP/FAQ/array_faq.html

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209
  • Thanks, Elena. The writeup is helpful but I am going to try the code offered by Ekkehard Horner first, since it does not rely on Jscript. – user1883779 Dec 07 '12 at 18:27