Solved

Listbox sorting numerically

Posted on 2004-08-17
6
305 Views
Last Modified: 2008-03-06
I am loading files from a directory into a Listbox using this:

Dim en As IEnumerator = OD1.FileNames.GetEnumerator
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.
0
Comment
Question by:JHalstead
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
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
     ListBox1.Items.Add(g)
Next
0
 

Author Comment

by:JHalstead
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

by:RonaldBiemans
ID: 11822080
oeps, sorry

dim str(0) as string
dim int(0) as integer
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:JHalstead
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)
        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.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

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

Accepted Solution

by:
Lunchy earned 0 total points
ID: 11824385
Closed, 125 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
update textboxes with Streamreader 21 47
Populating an array of locale_time_info 8 46
Run a batch file when a Crystal Report is opened 5 27
SqlServer no dupes 25 35
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question