Home All Groups Group Topic Archive Search About

Invert an Arraylist.Sort()

Author
6 Apr 2005 6:11 PM
Derek Martin
Hi List, I have an arraylist of objects.  I have created my own IComparable
in the object to return the sort on datetime.  This works great!  Now, I'd
like to invert the sort.  Currently, it gives me 4/1/05 before 4/5/05
(good), but I'd like to have 4/5/05 before 4/1/05 (better!) for my
application.

Public Function CompareTo(ByVal obj As Object) As Integer Implements + _
System.IComparable.CompareTo
    If TypeOf obj Is glcobject Then
        Dim tmp As glcobject = DirectCast(obj, glcobject)
        Return m_datetime.CompareTo(tmp.m_datetime)
    End If
End Function

???

Thanks!
Derek

Author
6 Apr 2005 6:23 PM
Marina
Maybe create a property which tells your object which way to sort. Then in
CompareTo look at that property, and return the appropriate result.

Show quoteHide quote
"Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
news:%23NGbTRtOFHA.3928@TK2MSFTNGP09.phx.gbl...
> Hi List, I have an arraylist of objects.  I have created my own
> IComparable in the object to return the sort on datetime.  This works
> great!  Now, I'd like to invert the sort.  Currently, it gives me 4/1/05
> before 4/5/05 (good), but I'd like to have 4/5/05 before 4/1/05 (better!)
> for my application.
>
> Public Function CompareTo(ByVal obj As Object) As Integer Implements + _
> System.IComparable.CompareTo
>    If TypeOf obj Is glcobject Then
>        Dim tmp As glcobject = DirectCast(obj, glcobject)
>        Return m_datetime.CompareTo(tmp.m_datetime)
>    End If
> End Function
>
> ???
>
> Thanks!
> Derek
>
Author
6 Apr 2005 6:24 PM
Russell Jones
Call Sort, and then ArrayList.Reverse.

Show quoteHide quote
"Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
news:%23NGbTRtOFHA.3928@TK2MSFTNGP09.phx.gbl...
> Hi List, I have an arraylist of objects.  I have created my own
> IComparable in the object to return the sort on datetime.  This works
> great!  Now, I'd like to invert the sort.  Currently, it gives me 4/1/05
> before 4/5/05 (good), but I'd like to have 4/5/05 before 4/1/05 (better!)
> for my application.
>
> Public Function CompareTo(ByVal obj As Object) As Integer Implements + _
> System.IComparable.CompareTo
>    If TypeOf obj Is glcobject Then
>        Dim tmp As glcobject = DirectCast(obj, glcobject)
>        Return m_datetime.CompareTo(tmp.m_datetime)
>    End If
> End Function
>
> ???
>
> Thanks!
> Derek
>
Author
6 Apr 2005 6:40 PM
Derek Martin
Ooooh - good idea!!!!  Thanks Russell...

Derek

Show quoteHide quote
"Russell Jones" <arj1@nospam.northstate.net> wrote in message
news:uSInuXtOFHA.1176@TK2MSFTNGP12.phx.gbl...
> Call Sort, and then ArrayList.Reverse.
>
> "Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
> news:%23NGbTRtOFHA.3928@TK2MSFTNGP09.phx.gbl...
>> Hi List, I have an arraylist of objects.  I have created my own
>> IComparable in the object to return the sort on datetime.  This works
>> great!  Now, I'd like to invert the sort.  Currently, it gives me 4/1/05
>> before 4/5/05 (good), but I'd like to have 4/5/05 before 4/1/05 (better!)
>> for my application.
>>
>> Public Function CompareTo(ByVal obj As Object) As Integer Implements + _
>> System.IComparable.CompareTo
>>    If TypeOf obj Is glcobject Then
>>        Dim tmp As glcobject = DirectCast(obj, glcobject)
>>        Return m_datetime.CompareTo(tmp.m_datetime)
>>    End If
>> End Function
>>
>> ???
>>
>> Thanks!
>> Derek
>>
>
>
Author
7 Apr 2005 12:31 AM
Dennis
If m_ascending Then
                        If Not v_CaseSensitive Then Return New
CaseInsensitiveComparer().Compare(x, y) Else Return New
Comparer(ci).Compare(x, y)
                    Else
                        If Not v_CaseSensitive Then Return New
CaseInsensitiveComparer().Compare(y, x) Else Return New
Comparer(ci).Compare(y, x)
                    End If

Show quoteHide quote
"Derek Martin" wrote:

> Ooooh - good idea!!!!  Thanks Russell...
>
> Derek
>
> "Russell Jones" <arj1@nospam.northstate.net> wrote in message
> news:uSInuXtOFHA.1176@TK2MSFTNGP12.phx.gbl...
> > Call Sort, and then ArrayList.Reverse.
> >
> > "Derek Martin" <dmj2195@DONTSPAMMEokstateDOT.edu> wrote in message
> > news:%23NGbTRtOFHA.3928@TK2MSFTNGP09.phx.gbl...
> >> Hi List, I have an arraylist of objects.  I have created my own
> >> IComparable in the object to return the sort on datetime.  This works
> >> great!  Now, I'd like to invert the sort.  Currently, it gives me 4/1/05
> >> before 4/5/05 (good), but I'd like to have 4/5/05 before 4/1/05 (better!)
> >> for my application.
> >>
> >> Public Function CompareTo(ByVal obj As Object) As Integer Implements + _
> >> System.IComparable.CompareTo
> >>    If TypeOf obj Is glcobject Then
> >>        Dim tmp As glcobject = DirectCast(obj, glcobject)
> >>        Return m_datetime.CompareTo(tmp.m_datetime)
> >>    End If
> >> End Function
> >>
> >> ???
> >>
> >> Thanks!
> >> Derek
> >>
> >
> >
>
>
>