Results 1 to 6 of 6

Thread: [RESOLVED] Double sorting of a list

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Resolved [RESOLVED] Double sorting of a list

    Hi all,

    I have been learning how to sort a list using lambda functions
    Code:
    excessPop.Sort(Function(f1, f2) f1.ID.CompareTo(f2.ID))
    I am however struggling to sort a list using 2 parameters within a lambda function. The properties within my list that i need to sort by are Category and then by Cost.

    Any pointers for how to do this?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Double sorting of a list

    This should work:
    vb.net Code:
    1. excessPop.Sort(Function(f1, f2) If(f1.Category.CompareTo(f2.Category) = 0, _
    2.                                    f1.Cost.CompareTo(f2.Cost), _
    3.                                    f1.Category.CompareTo(f2.Category))
    It's a bit clumsy because it will compare the Category values twice if they are not the same but, until .NET 4.0 and multiline lambdas, that's what you're stuck with.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Double sorting of a list

    thanks chcilhinney your a star one further question tho, what if i needed to sort by descending cost, how would i change the lambda expression to achieve this?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  4. #4

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Double sorting of a list

    worked it out thanks again for the help.

    wont let me rate you lol
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Double sorting of a list

    For completion this is how to change to descending for Cost
    vb.net Code:
    1. excessPop.Sort(Function(f1, f2) If(f1.Category.CompareTo(f2.Category) = 0, _
    2.                                    f2.Cost.CompareTo(f1.Cost), _
    3.                                    f1.Category.CompareTo(f2.Category))
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Double sorting of a list

    Quote Originally Posted by Megalith View Post
    thanks chcilhinney your a star one further question tho, what if i needed to sort by descending cost, how would i change the lambda expression to achieve this?
    Think about it. What does the CompareTo method actually do? It returns an Integer that indicates the relative order of the two values. Less than zero mean the first item comes before the second, greater than zero means the second item comes before the first and zero means that the two items are equivalent. With that in mind, what do you think you need to change here:
    Code:
    f1.Cost.CompareTo(f2.Cost)
    to make it sort in the opposite direction?

    That's how you need to approach problems like this. You don't just look at the code as a single unit. You break it down and ask yourself what each element of the code is actually doing. You can then look at how that fits into the whole and what adjustments you can make to change the way the whole works.

    EDIT: I guess you did think about it after all. You could have done that before I started typing out that long-winded response though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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