VBScript:从Scripting.Dictionary对项目进行排序 [英] VBScript: Sorting Items from Scripting.Dictionary

查看:93
本文介绍了VBScript:从Scripting.Dictionary对项目进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码...它捕获这样的数据:

I have the code below... it grabs data like this:

名称1,名称4,名称2,名称3

name1, name4, name2, name3

并列出以下内容([]是一个复选框):

and list like this ([ ] is a checkbox):

[]名称1 []名称4 []名称2 []名称3

[ ] name 1 [ ] name 4 [ ] name 2 [ ] name 3

<%
    Set DicionarioMyData = CreateObject("Scripting.Dictionary")
    Set MyData= TarefasConexaoMSSQL.Execute("SELECT A FROM TABLE")
    If Not MyData.EOF Then

        Do While Not MyData.EOF
            ItensDoMyData = MyData("A")

            If Not IsNull(ItensDoMyData) Then
                ItensSeparadosDoMyData = Split(ItensDoMyData, ",")

                For i = 0 To UBound(ItensSeparadosDoMyData)
                    ItensDoMyData = Trim(ItensSeparadosDoMyData(i))

                    If Not DicionarioMyData.Exists(ItensDoMyData) Then
                        DicionarioMyData.Add ItensDoMyData, i
                        %>
                  <input name="itens" type="checkbox" value="<% Response.Write ItensDoMyData %>"><label><% Response.Write ItensDoMyData %></label>
                        <%
                    End If
                Next
            End If
         MyData.MoveNext
    End If
%>

它正在工作,但我无法对其进行排序,因此正确的输出应为:

It's working, but i am not able to sort it, so the right output should be:

[]名称1 []名称2 []名称3 []名称4

[ ] name 1 [ ] name 2 [ ] name 3 [ ] name 4

是否可以对这种输出进行排序?

Is it possible to sort this kinda of output?

推荐答案

VBScript不能提供良好的排序选项,但是在任何较新的远程版本上,您都可以访问.NET提供的COM Visible类,其中之一是System.Collections.SortedList类.

VBScript doesn't offer good sorting options however on anything remotely modern you will have access to the COM Visible classes provided by .NET, one of which is the System.Collections.SortedList class.

因此,您的代码可以看起来像这样

Hence your code can look something more like this

Dim sl : Set sl = CreateObject("System.Collections.SortedList")

Dim rs : Set rs = conn.Execute("SELECT SomeField FROM SomeTable") 

If Not rs.EOF Then 

    Do While Not rs.EOF  
        If Not IsNull(rs("SomeField")) Then  
            AddStringListToSortedList rs("SomeField"), sl
        End If
    Loop

End If

rs.Close

For i = 0 To sl.Count - 1
    WriteCheckBox sl.GetKey(i)
Next

Sub AddStringListToSortedList(stringList, sortedList)

    Dim arr: arr = Split(stringList, ",")
    Dim i, item
    For i = 0 To UBound(arr)
        item = Trim(arr(i))
        If item <> "" Then
            If Not sortedList.Contains(item) Then
                sortedList.Add item, i
            End If
        End If
    Next

End Sub

Function WriteCheckbox(value)
%>
<input name="itens" type="checkbox" value="<%=Server.HTMLEncode(value)%>" /><label><%=Server.HTMLEncode(value) %></label>
<%
End Function  

这篇关于VBScript:从Scripting.Dictionary对项目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆