Sorting in date order

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 7
Reputation: Nubbing is an unknown quantity at this point 
Solved Threads: 0
Nubbing Nubbing is offline Offline
Newbie Poster

Sorting in date order

 
0
  #1
Mar 20th, 2006
Hello all. Pretty much finished my project for school, everything works, you can add and delete files, can't crash the program e.t.c. but I can't find out how to list these in order of date.

http://img300.imageshack.us/img300/4...sichelp5ck.png

Currently I have the sorted property of the list box equalling true. As you can see it orders the dates taking notice of the first number rather than the whole date. How can I make it order the files in order by date. By the way I am from the UK so the dates are written UK style way rather than the American way...

I am guessing I need to get the list box or a function to read the first 10 digits of each file, tell it to think of them as dates then order the files in that order...

If I can't do this I will have to include a search function which would display all the bookings made for that day and I am sure would be a lot harder

Thanks for looking. Also I would like to compliment the site designers as this forum has a very nice design. :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Sorting in date order

 
0
  #2
Mar 20th, 2006
You can use listview component.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Sorting in date order

 
0
  #3
Mar 20th, 2006
You'll have to do quite a bit of modification in your code to impliment this, since you already have it loading data and everything.... you'll need synchronized arrays to keep all your data in order, but here is a module I wrote that does bubble sort on dates, in either direction. The date format must be in the order of: dd/mm/yyyy. There are 3 functions, 2 are the bubble sort, and 1 (should be private, but what the hey) that is used to swap the elements to the respective indices. The 2 bubble sorts are: B2S_BSort_Date, and S2B_BSort_Date, which is Big To Small Bubble Sort, and Small To Big Bubble Sort Respectively. Based on the image you posted, you could easily modify the portions of the bubble sort in the nested loops that gather the portions of the date to work with your data without too much splitting.... but here is the project, and I wish you the best of luck.
Attached Files
File Type: zip Date_Sort.zip (2.0 KB, 83 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: Nubbing is an unknown quantity at this point 
Solved Threads: 0
Nubbing Nubbing is offline Offline
Newbie Poster

Re: Sorting in date order

 
0
  #4
Mar 21st, 2006
:p

Thanks a lot for the code, its great and it works... But I am not going to use it. Clearly it is too advanced for my level and if I put it in I don't think even my teacher would no what was happening.

There are so many things in the code that I don't understand, to name a few:

Public Sub B2S_BSort_Date(DList() As String, LBox As ListBox)

For I = 0 To UBound(DList) ' What does Ubound do

and I have never used a module before :eek: , as you can probably guess, my programming code is really messy and long. I'm 18 and not in uni yet so this probably explains most of my limited knowlage.

Actually looking more closely at it I may be able to use the code. Just tell me what the above does and what this procedure does:

Public Sub Swap(ByRef AName() As String, Indice1 As Integer, Indice2 As Integer)
Dim tmp As String

tmp = AName(Indice1)
AName(Indice1) = AName(Indice2)
AName(Indice2) = tmp

End Sub


Thanks a lot for all of your help.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: Nubbing is an unknown quantity at this point 
Solved Threads: 0
Nubbing Nubbing is offline Offline
Newbie Poster

Re: Sorting in date order

 
0
  #5
Mar 21st, 2006
Also although you said its for dates in the order dd/mm/yyyy

This program seems to be only for dates in format mm/dd/yyyy and it organises them in that order :-|

In fact I shall even quote one of your list items

X(4) = "06/15/1996"

I need it to work for dates in the format dd/mm/yyyy

and I can't work out how to modify your program in order to do this
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Sorting in date order

 
0
  #6
Mar 21st, 2006
Ok, to start, the line: Public Sub B2S_BSort_Date(DList() As String, LBox As ListBox), creates a public subroutine. You can create these on a whim just because you feel like it. They are user defined subroutines. You have to 2 kinds (well, a lot more in VB) of procedures. Subs and functions. These are sort of like mini-programs. Things that you will do in your code a bunch of times, but you don't want to bulk it out by copy and pasting the same code blocks over and over. So you just make a sub that does it once, and you call it whenever you need to. The name of the sub, B2S_BSort_Date, is Big To Small (B2S) Bubble Sort Date. When you call this sub (just like any other sub you would call, like print, or whatever) you have to give it information. In the case of this sub, we are passing it 2 "parameters". The first, is an array, and the second is a listbox. An array is a single variable, that can contain more than 1 value. So, for example, you can have variable X, and variable X at position 0 or X(0), can equal one thing... while variable X at position 1 X(1) can be something else altogether. The only restriction is that they be the same type (if X is integers, then all of them have to be integers). In this case, the variable that our sub expects is a string, containing dates in the form of dd/mm/yyyy. The listbox is just a reference so that the sub knows where to put the sorted information (I was going to have the function return a sorted array, but that gets a bit more complicated). When you actually call the sub, is makes a copy of the array passed to it. In the attached project, we pass X to the sub. VB copies all the values in X into DList for the sub to use, so in our Sub, all throughout it, we use DList, just as if it were X.... Moving on.

Now Arrays start their "slots" (indices, or elements) at position 0, not 1 (unless you specify otherwise in VB, but don't do that). So we For I = 0 To UBound(DList). Ubound is a function that returns the highest number of an array. For example, if we have an array, with X(0), X(1), X(2) and X(3), then ubound(X) would respond with 3. Because there are 4 (remember zero is a number) elements in our array, the highest number (the upper most element) is 3. This makes it possible for our sub (for the for loop) to work with numbers that change. For I = 0 to 10 is pretty limiting.... so we use ubound basically to say for each element of the array (for every value the array has) do this stuff. A module is just like a form, only it has no visual aspects to it. It's a place where you can store procedures (subs or functions) and variables that the entire project (more than 1 form) can use... if you had 3 forms, and you needed to call the same function from both of them, a module is perfect.

Now, This sub:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub Swap(ByRef AName() As String, Indice1 As Integer, Indice2 As Integer)
  2. Dim tmp As String
  3.  
  4. tmp = AName(Indice1)
  5. AName(Indice1) = AName(Indice2)
  6. AName(Indice2) = tmp
  7.  
  8. End Sub
Again, the first line tells VB this is a sub, named Swap. Remember how when we called the sort function, it made a copy of X into DList.... well, that's called "passing by value", which means it passes the value of the variable to the sub, but not the actual variable itself. This is so you can make changes to the variable all you want, but it won't affect the actual variable. So even though in our sort procedure, we change DList all around, it never actually changes X... well in this sub, we pass the array by reference. That means that the actual original variable gets changed, not just a copy of it. Then we pass the sub 2 integers, Indice1 and Indice2. Then we make a temporary variable, which holds the value of indice1, then we replace indice1 with indice2, and then indice2 with the value of the temporary variable. Basically, to break it down simply, this sub swaps 2 elements of an array, so pretend that we have X(0) and X(1). X(0) = "hi" and X(1) = "bye". If we call the sub swap, with: swap(X, 0, 1), then X(0) will NOW be "bye", and X(1) will now be "hi". It just swaps 2 elements of an array.

Breaking it all down, without so much technical crap , what we do basically is a nested for loop. That's a for loop, then inside of it, another for loop. And it is going over the same list, but this lets us compare everything in the array to itself. So, pretend we have X:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. X(0) = "hi"
  2. X(1) = "bye"
  3. X(2) = "something"
  4. X(3) = "else"
This system will say: ok, is X(0) greater than X(0), and if so, swap them. Is X(0) greater than X(1), if so swap them, is X(0) greater than X(2) if so, swap them, is X(0) greater than X(3), if so, swap them... is X(1) greater than X(0), if so swap them, is X(1) greater than X(1), if so, swap them, is X(1) greater than X(2), if so swap them.... so on, and so on, and so on. until everything is compared and swapped, at which point, you have a bubble sorted listbox.

Whew!
*Wipes His Forehead*
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Sorting in date order

 
0
  #7
Mar 21st, 2006
Crap, I see that, when I get home from work, I'll work on switching around the dates.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Sorting in date order

 
0
  #8
Mar 21st, 2006
I write simple date sort, hope you like it
Attached Files
File Type: zip date_sort.zip (1.3 KB, 48 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Sorting in date order

 
0
  #9
Mar 21st, 2006
it should work correctly..... all I did was take the nested for loop for months, and move it above the nested for loop for days, so instead of:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim I As Integer
  2. Dim X As Integer
  3. Dim IDay As String
  4. Dim XDay As String
  5. Dim IMonth As String
  6. Dim XMonth As String
  7. Dim IYear As String
  8. Dim XYear As String
  9.  
  10. ' /* Sort By Order Of Days*/
  11. For I = 0 To UBound(DList)
  12. For X = 0 To UBound(DList)
  13. IDay = Left(DList(I), 2)
  14. XDay = Left(DList(X), 2)
  15. If XDay < IDay Then
  16. Call Swap(DList(), I, X)
  17. End If
  18. Next X
  19. Next I
  20.  
  21. ' /* Sort By Order Of Months */
  22. For I = 0 To UBound(DList)
  23. For X = 0 To UBound(DList)
  24. IMonth = Mid(DList(I), 4, 2)
  25. XMonth = Mid(DList(X), 4, 2)
  26. If XMonth < IMonth Then
  27. Call Swap(DList(), I, X)
  28. End If
  29. Next X
  30. Next I
  31.  
  32. ' /* Sort By Order Of Years */
  33. For I = 0 To UBound(DList)
  34. For X = 0 To UBound(DList)
  35. IYear = Right(DList(I), 4)
  36. XYear = Right(DList(X), 4)
  37. If XYear < IYear Then
  38. Call Swap(DList(), I, X)
  39. End If
  40. Next X
  41. Next I

I made it:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim I As Integer
  2. Dim X As Integer
  3. Dim IDay As String
  4. Dim XDay As String
  5. Dim IMonth As String
  6. Dim XMonth As String
  7. Dim IYear As String
  8. Dim XYear As String
  9.  
  10. ' /* Sort By Order Of Months */
  11. For I = 0 To UBound(DList)
  12. For X = 0 To UBound(DList)
  13. IMonth = Mid(DList(I), 4, 2)
  14. XMonth = Mid(DList(X), 4, 2)
  15. If XMonth < IMonth Then
  16. Call Swap(DList(), I, X)
  17. End If
  18. Next X
  19. Next I
  20.  
  21. ' /* Sort By Order Of Days*/
  22. For I = 0 To UBound(DList)
  23. For X = 0 To UBound(DList)
  24. IDay = Left(DList(I), 2)
  25. XDay = Left(DList(X), 2)
  26. If XDay < IDay Then
  27. Call Swap(DList(), I, X)
  28. End If
  29. Next X
  30. Next I
  31.  
  32.  
  33. ' /* Sort By Order Of Years */
  34. For I = 0 To UBound(DList)
  35. For X = 0 To UBound(DList)
  36. IYear = Right(DList(I), 4)
  37. XYear = Right(DList(X), 4)
  38. If XYear < IYear Then
  39. Call Swap(DList(), I, X)
  40. End If
  41. Next X
  42. Next I

And that seems to work properly...

edit: oops, nevermind, still working on it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Sorting in date order

 
0
  #10
Mar 21st, 2006
Creative Visal, and Well done.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC