Sort a list of complex types in C sharp Arabic #77
Suggested Videos
❑ Sorting a list of complex types.
Sorting a list of complex types
To sort a list of complex types without using LINQ . The complex type has to implement Icomparable interface and provide Implementation for CompareTo() method .CompareTo() Method returns an integer, and the ,meaning of the return value is shown below .RETURN VALUE is
Greater than ZERO – the current instance is greater than the object being compared with . Less than ZERO – the current instance is less than the object being compared with.
Is ZERO – the current instance is equal to the object being compared with.
Alternatively you can also invoke CompareTo() Method . Salary property of the Customer object is int . Compare To() method is already implemented on integer type , so we can invoke this method and return it’s value.
If you prefer not to use the Sort functionality provided by the class , then you can provide your own , by implementing Icomparer interface . For example , if example , if I want the customers to sorted by name instead of salary.
Step 1: Implement Icomparer interface.
Step 2: Pass an instance of the class that implements Icomparer interface , as an argument to the Sort() method.
Tags:
C sharp