Results 1 to 4 of 4

Thread: [RESOLVED] List Max/Min vs. Sort

  1. #1
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 00
    Location
    Atlanta, Ga
    Posts
    380

    Resolved [RESOLVED] List Max/Min vs. Sort

    Hi All,
    I have a custom class and I have implemented iComparable for several fields via nested helper classes. (ala this article http://support.microsoft.com/kb/321292)

    I'd like to be able to use a function similar to the Array.Sort(Array, IComparer) with a list(of T). This works great with arrays, but I really prefer using lists.

    I'd also like to be able to use the list(of T).max/min functions as well with a specific IComparer rather than ONLY the compare method in my custom class.

    i.e. I'd like to be able to get the max/min of a list(of T) for more than just one field.

    Any good way to do that without writing a max/min function? (which I realize I could have written in the time to write this message... :-))

    Thanks!
    Nick

  2. #2
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,659

    Re: List Max/Min vs. Sort

    here's an example:

    vb.net Code:
    1. Public Class Class1
    2.     Public a As Integer
    3.     Public b As Decimal
    4.     Public c As String
    5. End Class

    vb.net Code:
    1. Public Class comparer
    2.     Implements IComparer(Of Class1)
    3.  
    4.     Private _field As String
    5.     Public Property field() As String
    6.         Get
    7.             Return _field
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _field = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Enum order
    15.         ascending
    16.         descending
    17.     End Enum
    18.  
    19.     Private _order As order
    20.     Public Property sortOrder() As order
    21.         Get
    22.             Return _order
    23.         End Get
    24.         Set(ByVal value As order)
    25.             _order = value
    26.         End Set
    27.     End Property
    28.  
    29.     Public Sub New(ByVal field As String, ByVal sortOrder As order)
    30.         Me.field = field
    31.         Me.sortOrder = sortOrder
    32.     End Sub
    33.  
    34.     Public Function Compare(ByVal x As Class1, ByVal y As Class1) As Integer Implements System.Collections.Generic.IComparer(Of Class1).Compare
    35.         If field = "a" Then
    36.             If Me.sortOrder = order.ascending Then
    37.                 Return CInt(x.a).CompareTo(CInt(y.a))
    38.             ElseIf Me.sortOrder = order.descending Then
    39.                 Return CInt(y.a).CompareTo(CInt(x.a))
    40.             End If
    41.         ElseIf field = "b" Then
    42.             If Me.sortOrder = order.ascending Then
    43.                 Return CDec(x.b).CompareTo(CDec(y.b))
    44.             ElseIf Me.sortOrder = order.descending Then
    45.                 Return CDec(y.b).CompareTo(CDec(x.b))
    46.             End If
    47.         ElseIf field = "c" Then
    48.             If Me.sortOrder = order.ascending Then
    49.                 Return x.c.CompareTo(y.c)
    50.             ElseIf Me.sortOrder = order.descending Then
    51.                 Return y.c.CompareTo(x.c)
    52.             End If
    53.         End If
    54.     End Function
    55. End Class

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private myList As New List(Of Class1)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         myList.Add(New Class1 With {.a = 5, .b = 1.5D, .c = "c"})
    7.         myList.Add(New Class1 With {.a = 3, .b = 0.75D, .c = "a"})
    8.         myList.Add(New Class1 With {.a = 7, .b = 2.25D, .c = "b"})
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         'to sort a generic list with an icomparer
    13.         myList.Sort(New comparer("c", comparer.order.ascending))
    14.         'you can use LINQ with min + max
    15.         MsgBox(myList.Min(Function(c1) c1.a))
    16.         MsgBox(myList.Max(Function(c1) c1.c))
    17.     End Sub
    18.  
    19. End Class

  3. #3

  4. #4
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 00
    Location
    Atlanta, Ga
    Posts
    380

    Re: List Max/Min vs. Sort

    Hi All,
    This works! Thanks Paul!
    Nick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •