Solved

Datewise Listbox Sorting in VB6

Posted on 2011-03-06
5
495 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
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:kbirecki
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That worked ..Thanks alot
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 48
VB6 - Compare and highlight cell not the same 3 33
Problem to start Neon 20 51
Not needed 13 53
This article will show, step by step, how to integrate R code into a R Sweave document
This is about my first experience with programming Arduino.
This video teaches viewers about errors in exception handling.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now