Results 1 to 9 of 9

Thread: Sorting list of objects

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    10

    Sorting list of objects

    Hi,

    I've created class which has 3 fields:
    • Name
    • Date and time of birth
    • Weight


    I create several objects and then put them into the list. Thanks to that later I can iterate list and display content of object on the asp page.

    It looks like table below:
    |Mike John|7/4/2012 6:55:31 AM|3450|
    |Lisa Ann|7/3/2012 6:43:31 AM|3650|
    |Betty|6/3/2012 8:47:31 AM|2850|

    User would like to have option to sort according to values one of these columns.

    How could I sort list of these objects?


    Regards,
    Lukasz

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Sorting list of objects

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim people As New List(Of person)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         people.Add(New person("Mike John", DateTime.Parse("7/4/2012 6:55:31 AM"), 3450))
    8.         people.Add(New person("Lisa Ann", DateTime.Parse("7/3/2012 6:43:31 AM"), 3650))
    9.         people.Add(New person("Betty", DateTime.Parse("6/3/2012 8:47:31 AM"), 2850))
    10.  
    11.     End Sub
    12.  
    13.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    14.         'the combobox contains 3 strings
    15.         ' 0 = byName
    16.         ' 1 = byDOB
    17.         '2 = byWeight
    18.  
    19.         'the checkbox specifies ascending (checked) or descending (unchecked)
    20.  
    21.         Select Case ComboBox1.SelectedIndex
    22.             Case 0
    23.                 If CheckBox1.Checked Then 'ascending
    24.                     people.Sort(Function(x, y) x.name.CompareTo(y.name))
    25.                 Else 'descending
    26.                     people.Sort(Function(x, y) y.name.CompareTo(x.name))
    27.                 End If
    28.             Case 1
    29.                 If CheckBox1.Checked Then 'ascending
    30.                     people.Sort(Function(x, y) x.DOB.CompareTo(y.DOB))
    31.                 Else 'descending
    32.                     people.Sort(Function(x, y) y.DOB.CompareTo(x.DOB))
    33.                 End If
    34.             Case 2
    35.                 If CheckBox1.Checked Then 'ascending
    36.                     people.Sort(Function(x, y) x.weight.CompareTo(y.weight))
    37.                 Else 'descending
    38.                     people.Sort(Function(x, y) y.weight.CompareTo(x.weight))
    39.                 End If
    40.         End Select
    41.  
    42.         For Each p As person In people
    43.             Debug.Print(p.ToString)
    44.         Next
    45.         Debug.Print("")
    46.  
    47.     End Sub
    48.  
    49. End Class

    this is the person class:

    vb Code:
    1. Public Class person
    2.     Public name As String
    3.     Public DOB As DateTime
    4.     Public weight As Integer
    5.  
    6.     Public Sub New(ByVal name As String, ByVal DOB As DateTime, ByVal weight As Integer)
    7.         Me.name = name
    8.         Me.DOB = DOB
    9.         Me.weight = weight
    10.     End Sub
    11.  
    12.     Public Overrides Function ToString() As String
    13.         Return String.Format("{0}, {1}, {2}", Me.name, Me.DOB, Me.weight)
    14.     End Function
    15.  
    16. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    10

    Re: Sorting list of objects

    Thank you very much!

    Lukasz

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting list of objects

    Paul, what unit of weight are you using?!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Sorting list of objects

    Quote Originally Posted by ForumAccount View Post
    Paul, what unit of weight are you using?!
    i dunno i just followed the OP's instructions...

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    10

    Re: Sorting list of objects

    Hi,

    How could I sort objects for example first according to the Name and then according to Weight? (I assume that there can be several people with the same name)

    Lukasz

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Sorting list of objects

    try this:

    Code:
    people.Sort(Function(x, y) If(x.name = y.name, x.weight.CompareTo(y.weight), x.name.CompareTo(y.name)))

  8. #8
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Sorting list of objects

    Quote Originally Posted by ForumAccount View Post
    Paul, what unit of weight are you using?!
    Here's a table listing average baby weight in grams by gestational age: http://www.babycenter.com/average-fe...h-weight-chart.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    10

    Re: Sorting list of objects

    Thanks Paul,

    Lukasz

Posting Permissions

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



Click Here to Expand Forum to Full Width