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.
我的code如下执行拆分命令:
My code executes a split command as follows:
outputArray =拆分(描述,)
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?
但我想诉诸数组,这样的话最长首先,即数组项是由字符串的长度排序。所以,有些VBScript中code,我似乎不能后弄清楚,数组是这样的:[描述,例如,这一点,是的,一]
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.
我将不胜AP preciate一些帮助在此从一个学生那里。谢谢你。
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).
ArrayList的VBScript中的排序是好简单的情况下而已,因为 - 据我所知 -
你不能传递一个比较函数的排序方法。请证明我错了!
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,考虑
的Schwartzian变换:
If you must use an ArrayList for more complex sorting, consider the Schwartzian transform:
- prepare定制的临时数据,以减轻比较
- 排序
- 恢复原始数据
在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)
输出:
A: this is an example of a description
B: description example this an is of a
这篇关于VBScript的编码挑战:拆分命令后数组排序按字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!