2

I am working with WPF and encountered a problem about sorting in a List. I find the sample that it use OrderBy( T => T.[the field that refer to it for sorting]) e.g.

List<Contact> contacts = new List<Contact>();
contacts.OrderBy(Contact => Contact.PhoneNumber)

It works perfectly.

However, if I don't know the type and even don't know the field in that type, how should I implement the sorting function?

Like: what should I put in in OrderBy(??? => ????.?????)

In fact, I wanna make a general GridView in which, when user click on one of the header, it will sort the whole list by the corresponding column.

Thank you so much!

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
user883434
  • 717
  • 2
  • 10
  • 25
  • 3
    How are you going to sort the list if you don't know the type of objects? What do you mean by sorting? – default locale Aug 08 '11 at 05:20
  • because I want to make a general sorting method in spite of the data the user input – user883434 Aug 08 '11 at 05:24
  • See if this helps. Looks some what similar. visit http://stackoverflow.com/questions/188141/c-list-orderby-alphabetical-order Hope it helps – Shadow Aug 08 '11 at 05:27

4 Answers4

2

There are two options.

  1. Use reflection to gather public properties which can be sorted on or
  2. Use classes (a good example would be DataTable) which enable you to access values through index ie. contacts.OrderBy(Contact => Contact[3])

Also see c# properties as array notation.

Option 1 expanded:

Getting an array of public properties seems a good choice and is quite easy:

var propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public | BindingFlags.Instance);

The resulting array can be used to populate the columns of your gridView and used to sort:

contacts.OrderBy(Contact => propertyInfos[0].GetValue(Contact, null));
Community
  • 1
  • 1
Goran
  • 6,798
  • 9
  • 41
  • 57
1

if you want to sort your datagrid, listview, listbox and so on. you should try ICollectionView. can you give some more information of what you want.

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • 1
    actually, I wanna make a general GridView with sorting function when users click on the header of each grid column. – user883434 Aug 08 '11 at 05:20
1

Two solutions for dynamic sorting ...

  1. GridView's ItemsSource is set to a ICollectionView or ListCollectionView with SourceCollection set as your own List and having a SortDescription where PropertyName can be specified as a string value... http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/

  2. Dynamic LINQ with MyList.AsQueryable().OrderBy(<OrderByClause>) where order by clause can be created dynamically... How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

So when user clicks on the grid view column headers, you have to extract the property name that the column is representing and use any of the two above approaches. In option 2 you would have to set the ItemsSource after the collection is sorted.

Any of the two approaches can be done in an Attached Behavior that works on GridViews in order to have a generic solution.

<ListView myNameSpace:ListSortingBehavior.SortingSource="CollectionView" ... /> <!-- CollectionView SortDescriptions -->

OR

<ListView myNameSpace:ListSortingBehavior.SortingSource="Generic" ... /> <!-- AsQueryable LINQ -->

All you have to worry about it if you use option1, then you would have to assume that the GridView.ItemsSource is always going to be an ICollectionView. In second approach the trade off is you would have to "set" the ItemsSource after sorting.

I hope this helps...

Community
  • 1
  • 1
WPF-it
  • 19,625
  • 8
  • 55
  • 71
0

You can use enum to make some types of sorting

Like

Sort.name
Sort.phone
Sort.adress

and the sorting just will be with letters and numbers and desc or asc

hope i have help

Alejandro Rangel
  • 1,670
  • 1
  • 20
  • 35