Find highest date
hi gurus. as the title suggests, i need to find the highest date in an arraylist.
i tried the sort function of the arraylist but it just sorted by days.
obviously life is not that easy right.
so now im trying to do it myself since all the sorters i found online cost me from $30 and up.
i add dates to the arraylist from a datareader like this
DaArr.Add(CType(dr.Item(0), Date).ToLongDateString)
then i try to find the highest date like this
Dim item, meti As Date
Dim count As Int16 = DaArr.Count
If count < 1 Then
DOLP = "Null"
Else
'Find highest date
Dim i As Int32
For i = 1 To count
meti = DaArr.Item(i - 1)
'Evaluate against date today
If meti >= Date.Today Or meti <= Date.Today Then
item = meti
If item < meti Then
DOLP = meti
Else
DOLP = item
End If
End If
Next
End If
'Return that date
Return DOLP
this doesnt work. im not sure what i should do now.
Please help if u can
TA
J!
# 1 Re: Find highest date
This was fun...
For this sample to work J, add 2 Listboxes to your form, and 1 button.
Declare this at the top of "Windows Form Designer generated code" :
Private dates() As Date
Add this to Button1_Click() :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReDim dates(9)
Dim rdDates As New Random 'Declare new random object
For i As Integer = 0 To 9
Dim d As Date = (Date.Today.AddDays(-rdDates.Next(0, 10000)))
ListBox1.Items.Add(d.ToString()) 'add to listbox
dates(i) = d
Next
' new instance of the custom sorter.
Dim sorter As New DateSorter 'see next code block
' sort with the custom sorter
Array.Sort(dates, sorter)
For i As Integer = 9 To 0 Step -1 'loop backwards, because highest date is last element
ListBox2.Items.Add(dates(i))
Next
MessageBox.Show("Highest Date = " & dates(9).ToString())
MessageBox.Show("Lowest Date = " & dates(0).ToString())
End Sub
This will add random dates into the first listbox - just so that we have a few dates to work with - you must add your own dates you want to test to an aary as well. Then the dates will get sorted and displayed from highest to lowest in the listbox2.
It will also show 2 messageboxes - one for the highest date, one for the lowest. :rolleyes: :)
This sorting, is actually done in the following class (which you can add to the bottom of your form's class). This class uses the IComparer Interface, which IMHO, is the best way to compare lists.. :
Public Class DateSorter
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
' we have to test x against y and return :
'<0 if x is less than y.
'0 if x equals y.
'>0 if x is greater than y.
' see msdn Icomparer.compare
Dim datex As Date = DirectCast(x, Date)
Dim datey As Date = DirectCast(y, Date)
' date.compare(t1, t2)
'Value Type Condition
'Less than zero t1 is less than t2.
'Zero t1 equals t2.
'Greater than zero t1 is greater than t2.
'
'so just:
Return Date.Compare(datex, datey)
End Function
End Class
I am attaching the VB 2003 sample with ;)
I hope it helps :D
# 2 Re: Find highest date
Thanks alot Hannes, my only question is that, can i do this from within a function.
and also i only have my array so would i just use the custom sorter like you have in the button click event.
Then the first date in the array should be the highest right?
Thanks.
# 3 Re: Find highest date
so basicly in my function i have now done this
[code]
''''
Dim sorter As New DateSorter
'sort with the custom sorter
DaArr.Sort(sorter)
DOLP = DaArr.Item(0)
Return DOLP
End Function
[code]
where DaArr is an arraylist not an array
Lets see if it works
# 4 Re: Find highest date
Your'e welcome dude! :)
If you have your own array, and implement the custom sorter similar to what I have done, it will work.
The first date, in my example, is the lowest, then it moves onwards to the highest. That's why I made the For loop loop backwards (from 9 to 0), in order to show the highest date first
# 5 Re: Find highest date
hey, there is one of these things uh what are they called again uh
problematic things uh
exceptions
it said invalid cast, so i changed directcast to ctype
now no more exception and it works, but it selectd the lowest date.
should i maybe set my variable = to the last in the array instead of the first?
# 6 Re: Find highest date
is this maybe breaking it.
its how i add dates to the array
DaArr.Add(CType(dr.Item(0), Date).ToLongDateString)
# 7 Re: Find highest date
hey, there is one of these things uh what are they called again uh
problematic things uh
exceptions:eek:
Mine was fine :) Does yours work now
should i maybe set my variable = to the last in the array instead of the first?
Yes :)
# 8 Re: Find highest date
Yes it works perfectly now, i dont think i need to change it from tolongdatestring but im not changing it back now in case something else goes wrong.
Thanks Alot
J
# 9 Re: Find highest date
That's good, J! Well done! :thumb:
Glad that I was able to help :)
# 10 Re: Find highest date
can do like [VB2005]
Dim lst As New List(Of DateTime)
lst.Add(New DateTime(2000, 12, 25))
lst.Add(New DateTime(2000, 12, 27))
lst.Add(New DateTime(2000, 11, 25))
lst.Sort()
MessageBox.Show("HighestDate is " & lst.Item(lst.Count - 1).ToShortDateString)
# 11 Re: Find highest date
jeez, now that seems quite a bit easier than 2003, but i dont have 2005, yet, when i get it ill be sure to use that way instead.
6 lines wow.
i wish the function i was bz with now was that short, up to 300 lines and it calls other functions(lots of them). getting a bit messy now :-)
Thanks ANI
J