Solved

Datewise Listbox Sorting in VB6

Posted on 2011-03-06
5
509 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

829 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