Results 1 to 10 of 10

Thread: Sort observablecollection by two fields?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Sort observablecollection by two fields?

    I have an observablecollection of classes that I would like to sort by two fields "Name" and "ID"

    I figured out how to sort by one field with
    Code:
    ProductCollection.OrderBy(Function(n) n.Name)
    But I cant quite figure out how to add "ID" as a second ordering field Any ideas?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Sort observablecollection by two fields?

    This is an example of why you should ALWAYS read the relevant documentation. If you had simply clicked on that OrderBy method and pressed F1 then you would have been taken directly to the documentation for that method. You would have then seen all the other members of the same type and, in that list, the ThenBy method.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,814

    Re: Sort observablecollection by two fields?

    Code:
    ProductCollection = ProductCollection.OrderBy(Function(n) n.Name).ThenBy(Function(n) n.ID)

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Sort observablecollection by two fields?

    Quote Originally Posted by .paul. View Post
    Code:
    ProductCollection = ProductCollection.OrderBy(Function(n) n.Name).ThenBy(Function(n) n.ID)
    For the record, that last assignment won't work if ProductCollection is type ObservableCollection(Of T), as is likely the case. It would need to be IEnumerable(Of T) for that assignment to work. I'm guessing that the OP actually wants an ObservableCollection(Of T) for its events. That would mean either sorting the data before adding it to the original collection or else sorting this data and then creating a new ObservableCollection(Of T) from the results. There's no easy way to sort an ObservableCollection(Of T) in place, as there is with an array or List(Of T).

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

    Re: Sort observablecollection by two fields?

    Quote Originally Posted by jmcilhinney View Post
    For the record, that last assignment won't work if ProductCollection is type ObservableCollection(Of T), as is likely the case. It would need to be IEnumerable(Of T) for that assignment to work. I'm guessing that the OP actually wants an ObservableCollection(Of T) for its events. That would mean either sorting the data before adding it to the original collection or else sorting this data and then creating a new ObservableCollection(Of T) from the results. There's no easy way to sort an ObservableCollection(Of T) in place, as there is with an array or List(Of T).
    Even with something simple like a bubbleSort?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Sort observablecollection by two fields?

    Quote Originally Posted by .paul. View Post
    Even with something simple like a bubbleSort?
    You can certainly implement your own sort on the collection but I was talking about something inbuilt when I said "easy way".

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Re: Sort observablecollection by two fields?

    I never knew that. I've learned a heck of a lot about VS and VB.NET in the last month. I was still thinking VB6 wise. Thanks I will try that - I hope it's better than google results!

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Sort observablecollection by two fields?

    Personally, I started using Bing a long time ago, specifically because I found the results better for .NET development. Not sure whether Microsoft gave it extra effort because they own both but, regardless, it always seemed to do a better job. I haven't used Google for a long time, so I'm not sure whether it's caught up in that regard. Either way, I'd always suggest reading the relevant documentation first, if you need help with a specific type or member. Context-sensitive Help is a good way to go straight to a specific page but I'd suggest bookmarking/favouriting the home page too. Not a day goes by that I don't use mine multiple times.

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    7,610

    Re: Sort observablecollection by two fields?

    Recently I've grown more fond of query syntax LINQ. I started preferring it to functional syntax because it tends to be more concise and a lot easier to follow. As such this:-
    Code:
    ProductCollection = ProductCollection.OrderBy(Function(n) n.Name).ThenBy(Function(n) n.ID)
    could be written like this:-
    Code:
    ProductCollection = From n In ProductCollection Order By n.Name, n.ID
    I would also like to point out that this sorting scheme would be meaningless if the collection never contains the same name multiple times with different IDs.
    Last edited by Niya; May 25th, 2022 at 03:21 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Re: Sort observablecollection by two fields?

    Well the click word and hit is helping in a lot of ways. I think that was the best answer to almost every question I have!

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