Vbscript 按名称对列表框条目排序

Vbscript 按名称对列表框条目排序,vbscript,hta,Vbscript,Hta,我已经设置了VBScript hta,让用户选择默认打印机。我目前正试图在视觉上尽可能清晰地表达出来 我们的打印机如下所示: 4th floor 1st floor Block 2 3rd floor etc. 有人知道我可以对代码进行更改以按名称对这些输出进行排序吗?非常感谢 <html> <head> <title>Choose your default printer</title> <style type="text/c

我已经设置了VBScript hta,让用户选择默认打印机。我目前正试图在视觉上尽可能清晰地表达出来

我们的打印机如下所示:

4th floor
1st floor
Block 2
3rd floor
etc.
有人知道我可以对代码进行更改以按名称对这些输出进行排序吗?非常感谢

<html> 
<head>
    <title>Choose your default printer</title> 
<style type="text/css"> 
    body { 
        font-family:Verdana; 
        font-size: 12px; 
        color: #49403B; 
        background: #FFFFFF; 
        text-align: center; 
        } 
</style> 

<SCRIPT Language="VBScript">
    Sub Window_Onload
        window.resizeTo 500,550
        strComputer = "."

        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

        Set re = New RegExp
        re.Pattern = "^\\\\.*?\\"

        For Each objPrinter in colPrinters
            strPrinter = objPrinter.Name
            Set objOption = Document.createElement("OPTION")
            objOption.Text = re.Replace(strprinter, "")
            objOption.Value = strPrinter
            If objPrinter.Default Then objOption.Selected = True
            AvailablePrinters.Add(objOption)
        Next
    End Sub

    Sub SetDefault
        strPrinter = AvailablePrinters.Value
        Set WshNetwork = CreateObject("Wscript.Network")
        WshNetwork.SetDefaultPrinter strPrinter
        Msgbox strprinter & " has been set as your default printer."
    End Sub
</SCRIPT>
Click a printer name to set it as your default<p>
<select size="20" name="AvailablePrinters" onChange="SetDefault"></select>
<p>
Close this window when done

您可以将打印机名称添加到数组中,然后使用简单的气泡排序。唯一棘手的部分是,您还可以获得默认打印机。因此,您必须在迭代Win32_打印机集合时保存该数据

' Create an array to hold the printer names...
ReDim a(colPrinters.count - 1)

' Add each printer name to the array. Also, save the name of the default printer.
Dim i, strDefault
For Each objPrinter In colPrinters
    a(i) = objPrinter.Name
    If objPrinter.Default Then strDefault = objPrinter.Name
    i = i + 1
Next

' Sort the array...
Sort a

For i = 0 To UBound(a)
    ' Create your <option>...
    If a(i) = strDefault Then objOption.Selected = True
Next

' Basic bubble sort...
Sub Sort(a)
    Dim i, j, temp
    For i = UBound(a) - 1 To 0 Step -1
        For j = 0 To i
            If a(j) > a(j + 1) Then
                temp = a(j + 1)
                a(j + 1) = a(j)
                a(j) = temp
            End If
        Next
    Next
End Sub
Set sortthing = CreateObject("System.Collections.SortedList")

下面是一个代码片段,它从stdin读取数据,添加到断开连接的记录集,对记录集进行排序,并将读写操作写入stdout

With rs
    .Fields.Append "SortKey", 201, 260 
    .Fields.Append "Txt", 201, 5000 
    .Open
    Do Until Inp.AtEndOfStream
        Lne = Inp.readline
        SortKey = Mid(Lne, Arg(3), Arg(4) - Arg(3))
        .AddNew
        .Fields("SortKey").value = SortKey
        .Fields("Txt").value = Lne
        .UpDate
    Loop
    If Arg(2) = "a" then SortColumn = "SortKey ASC"
    If Arg(2) = "d" then SortColumn = "SortKey DESC"
    .Sort = SortColumn
    Do While not .EOF
        Outp.writeline .Fields("Txt").Value
        .MoveNext
    Loop
End With
也可以使用.net排序数组/集合

' Create an array to hold the printer names...
ReDim a(colPrinters.count - 1)

' Add each printer name to the array. Also, save the name of the default printer.
Dim i, strDefault
For Each objPrinter In colPrinters
    a(i) = objPrinter.Name
    If objPrinter.Default Then strDefault = objPrinter.Name
    i = i + 1
Next

' Sort the array...
Sort a

For i = 0 To UBound(a)
    ' Create your <option>...
    If a(i) = strDefault Then objOption.Selected = True
Next

' Basic bubble sort...
Sub Sort(a)
    Dim i, j, temp
    For i = UBound(a) - 1 To 0 Step -1
        For j = 0 To i
            If a(j) > a(j + 1) Then
                temp = a(j + 1)
                a(j + 1) = a(j)
                a(j) = temp
            End If
        Next
    Next
End Sub
Set sortthing = CreateObject("System.Collections.SortedList")
请参阅.NET framework文档中的SortedList类。

我将使用和,如下所示:

Set printerNames = CreateObject("System.Collections.ArrayList")
Set printers     = CreateObject("Scripting.Dictionary")

For Each objPrinter in colPrinters
    name = re.Replace(objPrinter.Name, "")
    printerNames.Add name
    printers.Add name, objPrinter
Next

printerNames.Sort

For Each name in printerNames
    Set objOption = Document.createElement("OPTION")
    objOption.Text = name
    objOption.Value = printers(name).Name
    If printers(name).Default Then objOption.Selected = True
    AvailablePrinters.Add(objOption)
Next

您能告诉我在什么时候应该将其插入到现有代码中吗?许多的thanks@ITadminguy上述代码将替换HTA中的For Each循环。