Solved

# Listbox sorting numerically

Posted on 2004-08-17
301 Views
I am loading files from a directory into a Listbox using this:

Dim en As IEnumerator = OD1.FileNames.GetEnumerator
While en.MoveNext
End While

The file are named as such;  1x-111xx-1-1yy-5.toc.htm
I am needing to sort the files using the number after the yy-.  As 1,2,3,...9,10,11;  Right now sort is sorting alphabetically.

Any help on this wiuld be great, thanks.
0
• 3
• 2

LVL 25

Expert Comment

ID: 11821891
you could try something like this. (it's not brilliant code (redim) but it works and should give you a general idea)

dim str() as string
dim int() as integer
dim x as integer = 0
Dim en As IEnumerator = OD1.FileNames.GetEnumerator
While en.MoveNext
redim str(str.getupperbound(0) + 1)
redim int(int.getupperbound(0) + 1)
str(x) =  dir(en.current))
int(x) = Val(str(x).Substring(str(x).IndexOf("yy-") + 3, str(x).IndexOf(".") - (str(x).IndexOf("yy-") + 3)))
x += 1
End While

str.Sort(int, str)

For Each g As String In str
Next
0

Author Comment

ID: 11822041
Object reference not set to an instance of an object.  Occured on the first ReDim statemt.  str=nothing
0

LVL 25

Expert Comment

ID: 11822080
oeps, sorry

dim str(0) as string
dim int(0) as integer
0

Author Comment

ID: 11822202
Hi, I'm sorry to waist your time, but I got the answer from another post.  ICompare!!

Dim ar As New ArrayList(OD1.FileNames)
ar.Sort(New MySorter)

Private Class MySorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim s1 As String = x
Dim s2 As String = y
Try
Dim i1 As Integer = s1.IndexOf("."c)
Dim j1 As Integer = s1.LastIndexOf("-"c, i1)
Dim n1 As Integer = Integer.Parse(s1.Substring(j1 + 1, i1 - j1 - 1))

Dim i2 As Integer = s2.IndexOf("."c)
Dim j2 As Integer = s2.LastIndexOf("-"c, i2)
Dim n2 As Integer = Integer.Parse(s2.Substring(j2 + 1, i2 - j2 - 1))

Return n1 - n2
Catch ex As Exception
Return String.Compare(s1, s2, True)
End Try
End Function
End Class
0

LVL 25

Expert Comment

ID: 11822222
No problem, go to community service and get your points refunded
0

LVL 2

Accepted Solution

Lunchy earned 0 total points
ID: 11824385
Closed, 125 points refunded.
Lunchy
0

## Featured Post

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…