Solved

Datewise Listbox Sorting in VB6

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Starting up a Project

623 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