| | |
Sorting in date order
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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:
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:
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.
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
: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.
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.
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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
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:
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:
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*
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)
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
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)
X(0) = "hi" X(1) = "bye" X(2) = "something" X(3) = "else"
Whew!
*Wipes His Forehead*
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:
I made it:
And that seems to work properly...
edit: oops, nevermind, still working on it.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim I As Integer Dim X As Integer Dim IDay As String Dim XDay As String Dim IMonth As String Dim XMonth As String Dim IYear As String Dim XYear As String ' /* Sort By Order Of Days*/ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IDay = Left(DList(I), 2) XDay = Left(DList(X), 2) If XDay < IDay Then Call Swap(DList(), I, X) End If Next X Next I ' /* Sort By Order Of Months */ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IMonth = Mid(DList(I), 4, 2) XMonth = Mid(DList(X), 4, 2) If XMonth < IMonth Then Call Swap(DList(), I, X) End If Next X Next I ' /* Sort By Order Of Years */ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IYear = Right(DList(I), 4) XYear = Right(DList(X), 4) If XYear < IYear Then Call Swap(DList(), I, X) End If Next X Next I
I made it:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim I As Integer Dim X As Integer Dim IDay As String Dim XDay As String Dim IMonth As String Dim XMonth As String Dim IYear As String Dim XYear As String ' /* Sort By Order Of Months */ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IMonth = Mid(DList(I), 4, 2) XMonth = Mid(DList(X), 4, 2) If XMonth < IMonth Then Call Swap(DList(), I, X) End If Next X Next I ' /* Sort By Order Of Days*/ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IDay = Left(DList(I), 2) XDay = Left(DList(X), 2) If XDay < IDay Then Call Swap(DList(), I, X) End If Next X Next I ' /* Sort By Order Of Years */ For I = 0 To UBound(DList) For X = 0 To UBound(DList) IYear = Right(DList(I), 4) XYear = Right(DList(X), 4) If XYear < IYear Then Call Swap(DList(), I, X) End If Next X Next I
And that seems to work properly...
edit: oops, nevermind, still working on it.
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: hello
- Next Thread: Positioning a long text file in a Text Box
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






