Sorting using Different Properties of a C# Class

Zest4Quest 41 Reputation points
2021-02-04T14:08:19.053+00:00

Hi,
I have a C# class with props as below

class MyType
{
bool IsRestricted{}
bool IsVisible{}
bool IsSelected{}
long Count{}
… other props with different types are also here

}

I am using a collection of MyType in a grid. when i Click on the column header for IsSelected column, I want the sorting to happen based on the listed properties above. Ascending order should show IsVisible, IsRestricted, IsSelected == false, IsSelected == true (where isSelected == true is further sorted using Count)
Could someone please advice how I can do this? I am using a DevExpress grid, and it has a way of providing two consecutive rows, so that I can compare two rows. I just dont know how to implement sorting where we have to consider multiple fields. I hope it's possible.
Thanks
J

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,114 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 55,476 Reputation points
    2021-02-04T14:33:48.147+00:00

    DevExpress isn't part of .NET. You should really post in their support forum as how they implement sorting isn't necessarily how .NET does it.

    In Winform's version of DataGridView you can use the Sort method. This method has an overload taking an IComparer implementation which can sort by whatever you want. In fact that link to the Sort method has sample code doing exactly what you want. It sorts by column A and then B if column A are equal. That is what you'd do as well. Note that the sample implementation also takes into account whether you're ascending or descending sorting which is something you need to handle as well. Therefore I'd recommend you start with that example and adjust for your specific column sorting.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.