• 0

c# iComparer on bool and then date


Question

So I have a collection of custom objects that have two main properties (bool IsSticky and DateTime DateModified) to sort on. I'd like to sort the collection to keep the Stickies up top first and then sort by DateModified descending.

 

Should I use LINQ to simply do this or write a custom comparer to sort first on the boolean and then on the datetime?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Both ways will work. The custom comparer will be more efficient though because it'll avoid creating iterators and copying the results into a new collection at the end, but OrderBy().ThenBy() has a readability advantage. If the list needs to stay sorted at all times you could actually use a SortedList with a custom comparer.

Link to comment
Share on other sites

This topic is now closed to further replies.