Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bubble sort failed to sort properly

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
I tried to use the bubble sort function to sort a list of files, but the sort does not work properly. It sorts some files in the folder to a certain point and then it starts all over again.

I finally ended up using the Disconnected Record routine to sort files.

Any ideas why the bubble sort does work?. The sort code I have is listed below:

Dim arrItem, arrSortItem

strFolder = "C:\WINNT"
Set objFSO = CreateObject("Scripting.FileSystemObject")

arrItem = Array(1)
arrSortItem = Array(1)
i = 0
For Each strFile In objFSO.GetFolder(strFolder).Files
arrItem(i) = strFile.Name
i = i + 1
ReDim Preserve arrItem(i)
Next

arrSortItem = SortItem(arrItem)
For i = 0 To ubound(arrSortItem)
strFileName = arrSortItem(i)
Wscript.Echo strFileName
Next

'Function to sort the files
Function SortItem(arrSort)
Dim k, j, temp
For k = UBound(arrSort) - 1 To 0 Step -1
For j= 0 To k
If arrSort(j)>arrSort(j+1) Then
temp=arrSort(j+1)
arrSort(j+1)=arrSort(j)
arrSort(j)=temp
End If
Next
Next
SortItem = arrSort
End Function

Thanks!
 
i guess it might be how vbscript is implicity handling conversion before it does the > comparison?
 
What do you mean by, "It sorts some files in the folder to a certain point and then it starts all over again." ???

I was able to run your code and sort the files just fine.


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
And with this function ?
Function SortItem(arrSort)
Dim k, j, temp
For k = 0 To UBound(arrSort) - 1
For j= k + 1 To UBound(arrSort)
If arrSort(j)>arrSort(k) Then
temp=arrSort(j)
arrSort(j)=arrSort(k)
arrSort(k)=temp
End If
Next
Next
SortItem = arrSort
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The output from the script is shown below when I ran it against C:\WINNT on my computer:

0.log
002519_.tmp
Active Setup Log.BAK
Active Setup Log.txt
Awshkwv.ini
Blue Lace 16.bmp
C1Blue7456.bmp
Coffee Bean.bmp
DtcInstall.log
FaxSetup.log
FeatherTexture.bmp
Gone Fishing.bmp
Greenstone.bmp
IEPatchUninstall.log
Icccodes.dat
Iccsigs.dat
IsUninst.exe
KB810217.log
KB817778.log
KB822603.log
KB823182.log
KB824105.log
KB825119.log
KB826939.log
KB826942.log
KB828035.log
KB828741.log
KB833987.log
KB835732.log
KB837001.log
KB839643.log
KB839645.log
KB840315.log
KB840374.log
KB840987.log
KB841356.log
KB841533.log
KB841873.log
KB842773.log
KB867282-IE6SP1-20050127.163319.log
KB871250.log
KB873333.log
KB873339.log
KB873376.log
KB883939-IE6SP1-20050428.125228.log
KB885250.log
KB885835.log
KB885836.log
KB885884.log
KB886185.log
KB887472.log
KB887742.log
KB887797-OE6SP1-20041112.131144.log
KB888113.log
KB888302.log
KB890046.log
KB890047.log
KB890175.log
KB890859.log
KB891711.log
KB891781.log
KB892944.log
KB893066.log
KB893086.log
KB893756.log
KB893803v2.log
KB896358.log
KB896422.log
KB896423.log
KB896424.log
KB896426.log
KB896428.log
KB896727-IE6SP1-20050719.165959.log
KB897715-OE6SP1-20050503.210336.log
KB898461.log
KB899587.log
KB899588.log
KB899589.log
KB899591.log
KB900485.log
KB900725.log
KB901017.log
KB901214.log
KB902400.log
KB904706.log
KB905414.log
KB905495.log
KB905749.log
KB905915-IE6SP1-20051122.175908.log
KB905915.log
KB908519.log
KB908531.log
KB910437.log
KB911562.log
KB911564.log
KB911565.log
KB911567.log
KB911927.log
KB912812.log
KB912919.log
KB913446.log
KB913580.log
KB914798.log
Kpcms.ini
Kpcp32.dll
Kpfp32.dll
Kpscale.dll
Kpsharp.dll
Kpsys32.dll
MSI30-KB884016.log
MedCtrOC.log
ODBC.INI
ODBCINST.INI
OEWABLog.txt
Pcdlib32.dll
Prairie Wind.bmp
Ptpick32.dll
Q322011.log
Q327979.log
Q810400.log
Q813301.log
Q813586.log
Q814995.log
Q815485.log
Q816500.log
Q819696.log
Q828026.log
REGLOCS.OLD
Rhododendron.bmp
River Sumida.bmp
Santa Fe Stucco.bmp
SchedLgU.Txt
Soap Bubbles.bmp
Spwhpt.dll
Sti_Trace.log
TASKMAN.EXE
UNWISE.EXE
WGA.log
WMSysPr9.prx
WMSysPrx.prx
WgaNotify.log
Windows Update.log
WindowsShell.Manifest
WindowsUpdate.log
Zapotec.bmp
_default.pif
bootstat.dat
certmmc.log
clock.avi
cmsetacl.log
comsetup.log
control.ini
dahotfix.log
desktop.ini
entrust.001
entrust.ini
etinfo.cnt
etinfo.exe
etinfo.hlp
etinfo.ini
etinfres.dll
etlisrv.exe
etnewusr.exe
etoption.exe
etresusr.exe
etuninst.exe
etunres.dll
explorer.exe
explorer.scf
gary.ini
hh.exe
icccodes.dll
iis6.log
imsins.BAK
imsins.log
intelsds.log
jautoexp.dat
mdm.ini
mozver.dat
msdfmap.ini
msgsocm.log
msmqinst.log
netfxocm.log
notepad.exe
nsreg.dat
ntdtcsetup.log
ocgen.log
ocmsn.log
oeuninst.exe
oraodbc.ini
pfpick.dll
q330512.log
regedit.exe
regopt.log
sessmgr.setup.log
setdebug.exe
setupact.log
setupapi.log
setupapi.log.0.old
setupapi.log.1.old
setuperr.log
setuplog.txt
slrundll.exe
smscfg.ini
sprof32.dll
spupdsvc.log
svcpack.log
system.ini
tabletoc.log
tsoc.log
twain.dll
twain_32.dll
twunk_16.exe
twunk_32.exe
uninst.exe
updspapi.log
vb.ini
vbaddin.ini
vminst.log
vmmreg32.dll
wiadebug.log
wiaservc.log
win.ini
winhelp.exe
winhlp32.exe
winnt.bmp
winnt256.bmp
wmsetup.log
xpsp1hfm.log

As you can see, it sorted up to Zapotec.bmp, and then it started to begin the sortting again from:

_default.pif
bootstat.dat
certmmc.log

and so on.

I have run this script with less number of files and it did the same thing.

Thanks!
 
Simply replace this:
If arrSort(j)>arrSort(k) Then
with this:
If StrComp(arrSort(j),arrSort(k),1) > 0 Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Thank you for the response!
I replaced the code you suggested and the output I got was the following instead:

_default.pif
Zapotec.bmp
xpsp1hfm.log
WMSysPrx.prx
WMSysPr9.prx
wmsetup.log
winnt256.bmp
winnt.bmp
winhlp32.exe
winhelp.exe
WindowsUpdate.log
WindowsShell.Manifest
Windows Update.log
win.ini
wiaservc.log
wiadebug.log
WgaNotify.log
WGA.log
vmmreg32.dll
vminst.log
vbaddin.ini
vb.ini
updspapi.log
UNWISE.EXE
uninst.exe
twunk_32.exe
twunk_16.exe
twain_32.dll
twain.dll
tsoc.log
TASKMAN.EXE
tabletoc.log
system.ini
svcpack.log
Sti_Trace.log
Spwhpt.dll
spupdsvc.log
sprof32.dll
Soap Bubbles.bmp
smscfg.ini
slrundll.exe
setuplog.txt
setuperr.log
setupapi.log.1.old
setupapi.log.0.old
setupapi.log
setupact.log
setdebug.exe
sessmgr.setup.log
SchedLgU.Txt
Santa Fe Stucco.bmp
River Sumida.bmp
Rhododendron.bmp
regopt.log
REGLOCS.OLD
regedit.exe
Q828026.log
Q819696.log
Q816500.log
Q815485.log
Q814995.log
Q813586.log
Q813301.log
Q810400.log
q330512.log
Q327979.log
Q322011.log
Ptpick32.dll
Prairie Wind.bmp
pfpick.dll
Pcdlib32.dll
oraodbc.ini
OEWABLog.txt
oeuninst.exe
ODBCINST.INI
ODBC.INI
ocmsn.log
ocgen.log
ntdtcsetup.log
nsreg.dat
notepad.exe
netfxocm.log
msmqinst.log
MSI30-KB884016.log
msgsocm.log
msdfmap.ini
mozver.dat
MedCtrOC.log
mdm.ini
Kpsys32.dll
Kpsharp.dll
Kpscale.dll
Kpfp32.dll
Kpcp32.dll
Kpcms.ini
KB914798.log
KB913580.log
KB913446.log
KB912919.log
KB912812.log
KB911927.log
KB911567.log
KB911565.log
KB911564.log
KB911562.log
KB910437.log
KB908531.log
KB908519.log
KB905915.log
KB905915-IE6SP1-20051122.175908.log
KB905749.log
KB905495.log
KB905414.log
KB904706.log
KB902400.log
KB901214.log
KB901017.log
KB900725.log
KB900485.log
KB899591.log
KB899589.log
KB899588.log
KB899587.log
KB898461.log
KB897715-OE6SP1-20050503.210336.log
KB896727-IE6SP1-20050719.165959.log
KB896428.log
KB896426.log
KB896424.log
KB896423.log
KB896422.log
KB896358.log
KB893803v2.log
KB893756.log
KB893086.log
KB893066.log
KB892944.log
KB891781.log
KB891711.log
KB890859.log
KB890175.log
KB890047.log
KB890046.log
KB888302.log
KB888113.log
KB887797-OE6SP1-20041112.131144.log
KB887742.log
KB887472.log
KB886185.log
KB885884.log
KB885836.log
KB885835.log
KB885250.log
KB883939-IE6SP1-20050428.125228.log
KB873376.log
KB873339.log
KB873333.log
KB871250.log
KB867282-IE6SP1-20050127.163319.log
KB842773.log
KB841873.log
KB841533.log
KB841356.log
KB840987.log
KB840374.log
KB840315.log
KB839645.log
KB839643.log
KB837001.log
KB835732.log
KB833987.log
KB828741.log
KB828035.log
KB826942.log
KB826939.log
KB825119.log
KB824105.log
KB823182.log
KB822603.log
KB817778.log
KB810217.log
jautoexp.dat
IsUninst.exe
IP Address Mgmt.eras
intelsds.log
imsins.log
imsins.BAK
iis6.log
IEPatchUninstall.log
Iccsigs.dat
icccodes.dll
Icccodes.dat
hh.exe
Greenstone.bmp
Gone Fishing.bmp
gary.ini
FeatherTexture.bmp
FaxSetup.log
explorer.scf
explorer.exe
etunres.dll
etuninst.exe
etresusr.exe
etoption.exe
etnewusr.exe
etlisrv.exe
etinfres.dll
etinfo.ini
etinfo.hlp
etinfo.exe
etinfo.cnt
entrust.ini
entrust.001
DtcInstall.log
desktop.ini
dahotfix.log
control.ini
comsetup.log
Coffee Bean.bmp
cmsetacl.log
clock.avi
certmmc.log
C1Blue7456.bmp
bootstat.dat
Blue Lace 16.bmp
Awshkwv.ini
Active Setup Log.txt
Active Setup Log.BAK
002519_.tmp
0.log
 
Seems the test is inverted.
Replace the > with < (or vice-versa).

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

It still does not work either way.

Thanks anyway!
 
[1] Your establishing of the array is incorrect by one dimension in surplus with empty entry.
[tt]
[red]'[/red]arrItem = Array(1) 'what for?
[red]'[/red]arrSortItem = Array(1) 'what for
[blue]dim n: n=objFSO.GetFolder(strFolder).Files.count-1
redim arrItem(n)[/blue]
i = 0
For Each strFile In objFSO.GetFolder(strFolder).Files
arrItem(i) = strFile.Name
i = i + 1
[red]'[/red]ReDim Preserve arrItem(i)
Next
[/tt]
[2] The sorting behaviour seems normal. "it starts all over again" is a misleading description. The sort is just case sensitive, it is not starting all over again. The order is according to leading characters' ascii codes. If you want alphabetic case insensitive, you can make A-Za-z uniform case. Ucase or lcase depends on where you want to do with file name started by non-aphabetic characters before or after them. Like this.
[tt]
[red]'[/red]If arrSort(j)>arrSort(j+1) Then 'case sensitive sort
If lcase(arrSort(j))>lcase(arrSort(j+1)) Then 'case-insensitive sort
[/tt]
The rest I don't see what else meaning "it still does not work".
 
tsuji,

Thank you for the response!

It works now after I have made the change to reflect the case insensitive comparison and the array change you suggested. Nice catch!

So the final code then:
--------------------------------------------------------

Dim n, arrItem, arrSortItem

strFolder = "C:\WINNT"
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Sort the files with bubble sort
n = objFSO.GetFolder(strFolder).Files.count - 1
ReDim arrItem(n)
i = 0
For Each strFile In objFSO.GetFolder(strFolder).Files
arrItem(i) = strFile.Name
i = i + 1
Next

'Execute the bubble sort function
arrSortItem = SortItem(arrItem)

'Display the sorted items from the array
For i = 0 To UBound(arrSortItem)
strFileName = arrSortItem(i)
Wscript.Echo strFileName
Next

'Function to sort the files
Function SortItem(arrSort)
Dim k, j, temp
For k = UBound(arrSort) - 1 To 0 Step -1
For j= 0 To k
If Lcase(arrSort(j))>LCase(arrSort(j+1)) Then
temp=arrSort(j+1)
arrSort(j+1)=arrSort(j)
arrSort(j)=temp
End If
Next
Next
SortItem = arrSort
End Function

---------------------------------------------------------

Thanks again!

CluM09
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top