Solved

Datewise Listbox Sorting in VB6

Posted on 2011-03-06
5
526 Views
Last Modified: 2012-05-11
Hi,
I want to sort a listbox data datewise..
Listbox containing data like:

01-05-2004
10-11-2011
06-10-2009
26-01-1983
14-12-1966

I want to sort these listitems according to date..
Highest Date should be on top (Sorting in Descending order)

-Thanks :)
0
Comment
Question by:John-Paul75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:kbirecki
ID: 35053699
One way to do this is to create another column which is hidden and contains a calculated number upon which the sort is performed in "Ascending" order.  That column would contain the result of subtracting the date value for the row from a far future date, (i.e. {far future date} - {date for today} = X, while {far future date} - {date for yesterday} = X+1, a larger value).
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35053718
Are you loading the data from a database ? If so, just load them with an order by from the database.

Another thing that you can do is to loop the complete list and reorder them
Dim i As Integer, x As Integer, remove As Integer
Dim date1 As String

For i = List1.ListCount - 1 To 0 Step -1
  date1 = List1.List(i)
  remove = i
    For x = 0 To i - 1
     If DateValue(List1.List(x)) < DateValue(date1) Then
      date1 = List1.List(x)
      remove = x
     End If
    Next x
 List1.RemoveItem remove
 List1.AddItem date1
Next i

Open in new window

0
 

Author Comment

by:John-Paul75
ID: 35054237
Dhaest Thanks your code almost worked... but try these:

01-05-2004
11-11-2011
11-10-2011
10-11-2011
06-10-2009
26-01-1983
14-12-1966

Now after sorting I got


14-12-1966
26-01-1983
01-05-2004
06-10-2009
10-11-2011
11-10-2011
11-11-2011

Look at the last 3 dates

These should be:

11-10-2011
10-11-2011
11-11-2011


-Please try to solve it.

Thanks
regards,
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 35054318
That has something to do with your date-format on your system, because I don't have that issue here.

You can try to solve it with formatting the date
Private Sub OrderListBoxByDate()
Dim i As Integer, x As Integer, remove As Integer
Dim date1 As String

For i = List1.ListCount - 1 To 0 Step -1
      date1 = List1.List(i)
      remove = i
        For x = 0 To i - 1
         If DateValue(Format(List1.List(x), "DD/MM/YYYY")) < DateValue(Format(date1, "DD/MM/YYYY")) Then
          date1 = List1.List(x)
          remove = x
         End If
        Next x
     List1.RemoveItem remove
     List1.AddItem date1
    Next i
End Sub

Open in new window

0
 

Author Closing Comment

by:John-Paul75
ID: 35055318
That worked ..Thanks alot
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Six Sigma Control Plans

734 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