vbscript / hta Sorting a listbox

Hi All,

I am adding alot of items into a listbox and each item has a value associated with it. Does somebody already have a prebuilt function to sort a listbox after items are already added to it? Here is my code:


Set oWshNet = CreateObject("WScript.Network")
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objSWbemLocator.ConnectServer(hostname.value,"root\default",username.value,password.value,"MS_409","ntlmdomain:" + domain.value)
objSWbemServices.Security_.ImpersonationLevel = 3


Set oReg=objSWbemServices.get("stdregprov")
Set oMethod=oReg.methods_("EnumKey")
Set oInParameters=oMethod.inParameters.SpawnInstance_
oInParameters.hDefKey = HKEY_LOCAL_MACHINE
oInParameters.sSubKeyName = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"


Set oOutParameters=oreg.ExecMethod_(oMethod.name, oInParameters)
aKeys = oOutParameters.sNames

 Counter = 0
 
 
For Each sKey In aKeys
 

  Set objOption = Document.createElement("OPTION")
 
 

  Temp = oReg.GetStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + skey, "DisplayName" ,DisplayName)
  Temp = oReg.GetStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + skey, "UninstallString" ,UninstallString)
 
 

     Counter = Counter + 1
     objOption.Text = DisplayName
     objOption.Value = UninstallString
     apps.Add(objOption)
     appsfound.value = cstr(counter) + " applications found."
   
Next
WTarltonAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
OK, so you're going to have to place the DisplayName and UninstallString into that ArrayList, then sort, it, then write it back to the array, then populate the list box.....this will hopefully do that for you.

Regards,

Rob.
Set oWshNet = CreateObject("WScript.Network") 
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
 
Set objSWbemServices = objSWbemLocator.ConnectServer(hostname.value,"root\default",username.value,password.value,"MS_409","ntlmdomain:" + domain.value) 
objSWbemServices.Security_.ImpersonationLevel = 3
 
Const HKEY_LOCAL_MACHINE = &H80000002
 
Set oReg=objSWbemServices.get("stdregprov") 
Set oMethod=oReg.methods_.Item("EnumKey") 
Set oInParameters=oMethod.inParameters.SpawnInstance_ 
oInParameters.hDefKey = HKEY_LOCAL_MACHINE 
oInParameters.sSubKeyName = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
 
 
Set oOutParameters=oreg.ExecMethod_(oMethod.name, oInParameters) 
aKeys = oOutParameters.sNames 
 
Counter = 0
 
Set objDataList = CreateObject("System.Collections.ArrayList")
 
For Each sKey In aKeys
	Temp = oReg.GetStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + skey, "DisplayName" ,DisplayName)
	Temp = oReg.GetStringValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" + skey, "UninstallString" ,UninstallString)
	
	' Move the array elements to an ArrayList
	If DisplayName <> "" And UninstallString <> "" Then
	      objDataList.Add DisplayName & "|" & UninstallString
	End If
Next
 
' Sort the new array List
objDataList.Sort
' Move the sorted list back into the original array
sPrograms = ""
For Each sValue In objDataList
	If sPrograms =  "" Then
		sPrograms = sValue
	Else
		sPrograms = sPrograms & "^" & sValue
	End If
Next
 
aKeys = Split(sPrograms, "^")
 
WScript.Echo Join(aKeys, VbCrLf)
WScript.Quit
 
For Each sValue In aKeys
	DisplayName = Split(sValue, "|")(0)
	UninstallString = Split(sValue, "|")(1)
	Set objOption = Document.createElement("OPTION")
	Counter = Counter + 1
	objOption.Text = DisplayName
	objOption.Value = UninstallString
	apps.Add(objOption)
Next
appsfound.value = cstr(counter) + " applications found."

Open in new window

0
 
RobSampsonCommented:
Hi, with an Array, if you have the .NET Framework 2.0 installed, you can use the
System.Collections.ArrayList
object to sort elements.

Here's an example that will takes the values from an array (aKeys), puts them into an ArrayList object, sorts them, then puts the sorted values back into the array.  You can then populate your list box with the sorted array as normal.

'===============
aKeys = Array("D","C","Z","A")
Set objDataList = CreateObject("System.Collections.ArrayList")
' Move the array elements to an ArrayList
For intCount = LBound(aKeys) To UBound(aKeys)
      objDataList.Add aKeys(intCount)
Next
' Sort the new array List
objDataList.Sort
' Move the sorted list back into the original array
For intCount = LBound(aKeys) To UBound(aKeys)
      aKeys(intCount) = objDataList(intCount)
Next
MsgBox Join(aKeys, VbCrLf)
'===============

Regards,

Rob.
0
 
WTarltonAuthor Commented:
The problem is that aKeys is not the array I want to sort. The value display name inside each one of the aKeys is what im trying to sort. I have been thinking about it and I think I either A have to sort whats in my listbox or B create 2 arrays one for the display name and one for the unistall string and sort them by array a and just have array b mimic the sorting of a to keep the values associated with the correct member.
0
 
WTarltonAuthor Commented:
Your the bomb that worked great!!!!
0
 
RobSampsonCommented:
Thanks for the grade.  This was a good one!  I had to wrap my brain around this, but it seems a pretty good solution.

Regards,

Rob.
0
All Courses

From novice to tech pro — start learning today.