VBScript Coding Challenge: Sort Array by String Length after Split Command

冷暖自知 提交于 2020-01-19 06:22:08

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/13753130/vbscript-coding-challenge-sort-array-by-string-length-after-split-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!