Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4341850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:32:23+00:00 2026-05-21T11:32:23+00:00

I have a ListView (GridView) that I want to sort by 2 columns, so

  • 0

I have a ListView (GridView) that I want to sort by 2 columns, so if 2+ items have the same value in Column 1, it sorts by Column 2. Pretty easy. But empty strings show up at the top when sorting A-Z. I’d like to move them to the bottom. I made a comparer (IComparer) that takes care of this, but I’m not sure how to use it.

Here’s the code I tried:

 Dim view As ListCollectionView = CollectionViewSource.GetDefaultView(myCollection)
 Using view.DeferRefresh
    view.SortDescriptions.Clear()
    view.SortDescriptions.Add(New SortDescription(sortHeader.Header, direction))
    view.SortDescriptions.Add(New SortDescription(otherColumn, direction))
    view.CustomSort = New MyComparer()
 End Using

The problem is my comparer is given a type of my class instead of the value of the property/column being sorted by. So if the class is Foo and I’m sorting by Foo.Bar, I get the entire Foo class, not just the value of Bar (which is really all it should be concerned about, since that’s what it’s sorting by).

How will my comparer know which property to compare? Maybe I’m doing something wrong here, because this doesn’t make any sense. I expected to get a String (the property type) for x and y…

Does anyone know how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:32:24+00:00Added an answer on May 21, 2026 at 11:32 am

    Your IComparer implementation will be given the entire object, you need to figure out which column gets clicked, probably by doing something like this:

    this.AddHandler(GridViewColumnHeader.ClickEvent, 
                    new RoutedEventHandler(Column_Sort));
    

    and then feed that into your MyComparer probably by modifying your constructor to take in the property path.

    In Column_Sort you can get the property path something like this (I’m a little rusty on vb, but c# would do this:

    void Column_Sort(object sender, RoutedEventArgs e)
    {
      var memberBinding= ((GridViewColumnHeader)e.OriginalSource).Column.DisplayMemberBinding;
      var path = ((Binding)memberBinding).Path.Path;
    }
    

    and then feed that into your sorting logic.

    Dim view As ListCollectionView = CollectionViewSource.GetDefaultView(myCollection)
     Using view.DeferRefresh
        view.SortDescriptions.Clear()
        view.SortDescriptions.Add(New SortDescription(sortHeader.Header, direction))
        view.SortDescriptions.Add(New SortDescription(otherColumn, direction))
        view.CustomSort = New MyComparer(PropertyPath)
     End Using
    

    EDIT:
    You just need to customize your IComparer to support multiple column sorting, I googled and found this comparer implementation that you can feed it several property paths separated by commas and suffixed with ASC/DESC like this:

    User.LastName DESC, User.FirstName DESC
    

    I think the convention for multi-column sorting as far as UI is concerned is that you need to have the Ctrl button held. So subclass the ListView class and tie into the GridViewColumnHeader clicked event and the KeyDown event to know which columns were clicked in succession and then use an IComparer implementation to feed that into.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView in ASP.NET where one column consists checkBoxes. I want the
I bind List of items to WPF ListView and want to set row background/foreground
I have a listview that is populated by a by objects from an Offer
I have a ListView that uses a custom adapter. The custom adapter's getView uses
I have a listview that is setup like so: <asp:ListView ID=lv_First runat=server> <LayoutTemplate> <div
I have a ListView that looks like this: Notice how the TextView is getting
I have a ListView where each item is a TextView . I want to
I have listview with some listadapter after click on item from list I'd like
I have a ListView Control bound to a ListCollectionView in a ViewModel. I wanted
I've read in the Android 3.0 documentation that it is now possible to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.