Home All Groups Group Topic Archive Search About
Author
11 Sep 2006 5:25 PM
vzsnake
Hello,everybody.

One my class inhereted from System.Collections.CollectionBase in
InnerList I add class B objects to InnerList and want to sort them by
multiple properties. I done the sorting, BUT when my classes have equal
values in property Name, and I sort the list by "name desc" or "name
asc", It sort wrong!
For example: (start list)
A
B
C

After Sort:
A
C
B

Can anyone explain why does it happen ?
Thanks.

Author
11 Sep 2006 5:59 PM
Michael D. Ober
Which version of the framework?  v2 has a SortedList(of <type>) generic
class that you can use.

Mike Ober.

<vzsn***@gmail.com> wrote in message
Show quoteHide quote
news:1157995534.673061.223140@i42g2000cwa.googlegroups.com...
> Hello,everybody.
>
> One my class inhereted from System.Collections.CollectionBase in
> InnerList I add class B objects to InnerList and want to sort them by
> multiple properties. I done the sorting, BUT when my classes have equal
> values in property Name, and I sort the list by "name desc" or "name
> asc", It sort wrong!
> For example: (start list)
> A
> B
> C
>
> After Sort:
> A
> C
> B
>
> Can anyone explain why does it happen ?
> Thanks.
>
Author
11 Sep 2006 6:04 PM
vzsnake
Framework 1.1
Author
11 Sep 2006 6:06 PM
vzsnake
Is any chanse to correct this error in framework 1.1? vzsn***@gmail.com писал(а): > Framework 1.1
Author
11 Sep 2006 6:35 PM
Kerry Moorman
vzsnake,

What code are you using to sort "by multiple properties"?

Kerry Moorman

Show quoteHide quote
"vzsn***@gmail.com" wrote:

> Hello,everybody.
>
> One my class inhereted from System.Collections.CollectionBase in
> InnerList I add class B objects to InnerList and want to sort them by
> multiple properties. I done the sorting, BUT when my classes have equal
> values in property Name, and I sort the list by "name desc" or "name
> asc", It sort wrong!
> For example: (start list)
> A
> B
> C
>
> After Sort:
> A
> C
> B
>
> Can anyone explain why does it happen ?
> Thanks.
>
>
Author
11 Sep 2006 6:50 PM
vzsnake
Here's  code:

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collections.IComparer.Compare

            Dim retVal As Integer = 0


            For i As Integer = 0 To Me._props.Length - 1
                If (retVal = 0) Then
                    Dim sortStr As String()
                    sortStr = Regex.Replace(_props(i).Trim(), "\s{1,}",
" ").Trim.Split(" ".ToCharArray())

                    Dim orderAsc As Boolean = True
                    If (sortStr.Length > 1) Then
                        orderAsc = GetOrderType(sortStr(1))
                    End If
                    Dim field As String = sortStr(0)


                    Dim prop As PropertyInfo =
x.GetType().GetProperty(field.Trim(), BindingFlags.IgnoreCase Or
BindingFlags.GetProperty Or BindingFlags.Public Or
BindingFlags.Instance)

                    Dim valx As Object = prop.GetValue(x, Nothing)
                    Dim valy As Object = prop.GetValue(y, Nothing)


                    '  If (CType(valx, String) <> CType(valy, String))
Then
                    If (orderAsc) Then
                        retVal = CType(valx,
String).Trim().CompareTo(CType(valy, String).Trim())
                    Else
                        retVal = CType(valy,
String).Trim().CompareTo(CType(valx, String).Trim())
                    End If
                    ' End If

                End If


            Next

            Return retVal
        End Function
Author
12 Sep 2006 1:56 PM
Claes Bergefall
I tested your code and it seem to work just fine in both 1.1 and 2.0. Since
you didn't include the code for _props and GetOrderType I implemented them
like this:

Private _props() As String = New String() {"name desc"}

Private Function GetOrderType(ByVal value As String) As Boolean
    If value = "asc" Then
        Return True
    Else
        Return False
    End If
End Function

Then I created a small class with a name property and added a bunch of them
to an ArrayList (which is what the InnerList property returns) and called
sort with your Compare method. Worked just fine.

   /claes

<vzsn***@gmail.com> wrote in message
Show quoteHide quote
news:1158000634.147041.70480@d34g2000cwd.googlegroups.com...
> Here's  code:
>
> Public Function Compare(ByVal x As Object, ByVal y As Object) As
> Integer Implements System.Collections.IComparer.Compare
>
>            Dim retVal As Integer = 0
>
>
>            For i As Integer = 0 To Me._props.Length - 1
>                If (retVal = 0) Then
>                    Dim sortStr As String()
>                    sortStr = Regex.Replace(_props(i).Trim(), "\s{1,}",
> " ").Trim.Split(" ".ToCharArray())
>
>                    Dim orderAsc As Boolean = True
>                    If (sortStr.Length > 1) Then
>                        orderAsc = GetOrderType(sortStr(1))
>                    End If
>                    Dim field As String = sortStr(0)
>
>
>                    Dim prop As PropertyInfo =
> x.GetType().GetProperty(field.Trim(), BindingFlags.IgnoreCase Or
> BindingFlags.GetProperty Or BindingFlags.Public Or
> BindingFlags.Instance)
>
>                    Dim valx As Object = prop.GetValue(x, Nothing)
>                    Dim valy As Object = prop.GetValue(y, Nothing)
>
>
>                    '  If (CType(valx, String) <> CType(valy, String))
> Then
>                    If (orderAsc) Then
>                        retVal = CType(valx,
> String).Trim().CompareTo(CType(valy, String).Trim())
>                    Else
>                        retVal = CType(valy,
> String).Trim().CompareTo(CType(valx, String).Trim())
>                    End If
>                    ' End If
>
>                End If
>
>
>            Next
>
>            Return retVal
>        End Function
>
Author
20 Sep 2006 8:14 PM
vzsnake
Big Thanks, Claes Bergefall for your help

But I still had to rewrite my sorting using my own stable sorting - I
used MergeSort.