VBScript 编码挑战:拆分命令后按字符串长度对数组进行排序 [英] VBScript Coding Challenge: Sort Array by String Length after Split Command
问题描述
我是 VBScript 编码的 C 学生,需要一点帮助.
I am a C student in VBScript coding and need a little help.
我的代码执行拆分命令如下:
My code executes a split command as follows:
outputArray = split(Description," ")
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.
因此,例如,如果 Description 等于这是一个描述的示例",那么我的数组值是 [this, is, an, example, of, a, description],对吗?
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?
但我想对数组进行排序,以便最长的单词在前,即数组项按字符串长度排序.因此,在我似乎无法弄清楚的一些 VBScript 代码之后,该数组将如下所示:[描述,示例,这个,一个,是,的,一个]
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.
我非常感谢那里的 A 学生在这方面的帮助.谢谢.
I would greatly appreciate some help on this from an A student out there. Thanks.
推荐答案
由于 VBScript 没有本地排序,它需要朋友的帮助.在您的情况下 - 由于您的排序标准更复杂 - 朋友不应该是 .Net 的 ArrayList、JScript 的排序或 sort.exe (此处介绍),但是一个断开的ADO记录集:
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)
输出:
A: this is an example of a description
B: description example this an is of a
对于后台启动这里.
添加 - 对于 ArrayList 爱好者:
断开的记录集应该是您的首选,如果您的数据本质上是表格(排序标准涉及元素的多个方面/属性).
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).
VBScript 中的 ArrayList 排序仅适用于简单情况,因为 - AFAIK -您不能将比较函数传递给 sort 方法.请证明我错了!
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!
如果必须使用 ArrayList 进行更复杂的排序,请考虑施瓦兹变换:
If you must use an ArrayList for more complex sorting, consider the Schwartzian transform:
- 准备自定义的临时数据以简化比较
- 排序
- 恢复原始数据
在代码中:
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)
输出:
A: this is an example of a description
B: description example this an is of a
这篇关于VBScript 编码挑战:拆分命令后按字符串长度对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!