Sort a list of simple types in C sharp Arabic #76
Suggested Videos
❑ Sorting a list of simple types .
Sorting a list of simple types in C#
Sorting a list of simple types like int, string char etc , is straight forward . Just invoke the sort() method on the list instance and the data will be automatically sorted in ascending order.
If you want the data to be retrieved in descending order, use Reverse() method on the list instance .
However, when you do the same thing on a complex type like Customer , we get a runtime invalid operation exception – Failed to compare 2 elements in the array . This because ,.NET runtime does not know ,how to sort complex types, we have to tell the way we want data to be sorted in the list by implementing IComparable interface.
How is sort functionality working for simple types like int ,sting , char ,etc? That is because these types (int ,string ,decimal ,char etc) have implemented IComparable interface already .
Tags:
C sharp