我需要帮助编写一个函数,该函数将接受一个字符串数组并将其排序为一个新的字符串数组,该数组仅基于字符串中的数字字符(升序)?假设我有以下字符串数组:
"abc53ddd", "2zzz2yyy", "14"
我需要将它们列为
"14", "2zzz2yyy" and "abc53ddd"
由于字符串中的数字字符是14, 22 and 53.
我可以将字符串剥离成数字部分并对数组进行排序,但我无法恢复剩余的字符以正确的顺序列出它们......
Use ADO with disconnected recordset. See http://technet.microsoft.com/en-us/library/ee176578.aspx. It is fast and robust. IMHO, it is the best.
'recordset definition
Dim rst As New ADODB.Recordset 'must be reference "Microsoft ActiveX Data Objects"
'OR use automation (without reference)
'Dim rst
'Set rst = CreateObject("ADOR.Recordset")
'fields definition - how many you want (name, type, lenght)
rst.Fields.Append "field1", ADODB.DataTypeEnum.adVarChar, 255
rst.Fields.Append "number", ADODB.DataTypeEnum.adInteger
rst.Open
'add values, for example:
Dim rc As ADODB.Record
rst.AddNew "field1", "abc53ddd"
rst.AddNew "field1", "2zzz2yyy"
rst.AddNew "field1", "14"
'make numbers from string and store in field [number]
Dim reg As New RegExp
'OR
'Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "\D" 'non-digit
reg.Global = True
rst.MoveFirst
Do Until rst.EOF
rst!Number = Val(reg.Replace(rst!field1, "")) 'delete non-digit
rst.MoveNext
Loop
'sort!
rst.Sort = "number ASC" 'sort by field1 ascending
'show output
rst.MoveFirst
Do Until rst.EOF
Debug.Print rst!field1, rst!Number
rst.MoveNext
Loop