2

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

name1, name4, name2, name3

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

[ ] 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:

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

Is it possible to sort this kinda of output?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Khrys
  • 2,670
  • 9
  • 49
  • 82

1 Answers1

1

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  
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • Thanks for the code... i tried to run it... and it keep processing without showing any results... And than i get a timeout... the query i am doing is pretty simple and in the original code it runs almost instantantly. Thanks again. – Khrys Jul 04 '12 at 11:55
  • btw, i really needs to keep it in Classic ASP. I am trying to use this script: http://blogcastrepository.com/blogs/mattbro/archive/2006/11/10/2603.aspx still no lucky yet... – Khrys Jul 04 '12 at 12:10
  • "I really need to keep it in Classic ASP", well by that definition "Scripting.Dictionary" is not "in" classic ASP either. You are sure that your server is so old that it doesn't have this class present? If not why is the SortedList COM class any less "ASP" than "Scripting.Dictionary" simply because its written in .NET? – AnthonyWJones Jul 04 '12 at 12:15
  • Well, my whole code is in Classic ASP... i wasn't sure if System.Collections.SortedList could make it not work... I just tells that to clarify. Thanks for your time again. – Khrys Jul 04 '12 at 12:18
  • BTW the code in that link is just plain messed up. If you really must do it in "ASP" then here the definitive approach http://support.microsoft.com/kb/246067 – AnthonyWJones Jul 04 '12 at 12:20
  • @Khrys: As I'm trying to point out as soon as you use anything outside of ASP/Script such as "Scripting.Dictionary", "ADODB", "MSXML" you are using a COM objects that just happen to be available on the server. These days .NET 2.0 is also pretty ubiquitous and hence any COM object that is available in .NET are fair game too. – AnthonyWJones Jul 04 '12 at 12:23
  • Thanks for your help... I will check the Microsoft website. Thanks again. – Khrys Jul 04 '12 at 12:38