JHalstead
asked on
Listbox sorting numerically
I am loading files from a directory into a Listbox using this:
Dim en As IEnumerator = OD1.FileNames.GetEnumerato r
While en.MoveNext
ListBox1.Items.Add(Dir(en. Current))
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.
Dim en As IEnumerator = OD1.FileNames.GetEnumerato
While en.MoveNext
ListBox1.Items.Add(Dir(en.
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.
ASKER
Object reference not set to an instance of an object. Occured on the first ReDim statemt. str=nothing
oeps, sorry
dim str(0) as string
dim int(0) as integer
dim str(0) as string
dim int(0) as integer
ASKER
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)
ListBox1.Items.AddRange(ar .ToArray() )
Private Class MySorter
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.ICompar er.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
Dim ar As New ArrayList(OD1.FileNames)
ar.Sort(New MySorter)
ListBox1.Items.AddRange(ar
Private Class MySorter
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.ICompar
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
Dim i2 As Integer = s2.IndexOf("."c)
Dim j2 As Integer = s2.LastIndexOf("-"c, i2)
Dim n2 As Integer = Integer.Parse(s2.Substring
Return n1 - n2
Catch ex As Exception
Return String.Compare(s1, s2, True)
End Try
End Function
End Class
No problem, go to community service and get your points refunded
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dim str() as string
dim int() as integer
dim x as integer = 0
Dim en As IEnumerator = OD1.FileNames.GetEnumerato
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
x += 1
End While
str.Sort(int, str)
For Each g As String In str
ListBox1.Items.Add(g)
Next