Hence, the delegate points to our new method. We can then create an instance of the Comparison delegate and pass CompareBooks as an argument. Let’s start by creating a method that matches the signature of the Comparison delegate: public static int CompareBooks(Book x, Book y) We can use this in addition to the other approaches to sort the list. Using the Comparison DelegateĪn overload of the Sort() method expects Comparison delegate as an argument. NET/C# content and get paid? > JOIN US! sorts the list in place and doesn’t create a new copy of the original list. Wanna join Code Maze Team, help us produce more awesome. Now, we need to pass an instance of the SortBookByTitle class as an argument to the Sort() method: Let’s implement the IComparer to sort the List by Title: public int Compare(Book x, Book y) IComparer, on the other hand, provides us with a mechanism to compare two different objects. comparing another instance to the current instance. We use IComparable for intrinsic comparison, i.e. The IComparer and IComparable interfaces work differently. We can also use IComparer interface to have our comparison implementations and hence, sort the list according to our custom implementations. When we invoke the Sort() method on the original unsorted List, it is sorted without needing to create a separate copy of the original list: _books.Sort() ĬollectionAssert.AreEqual(_booksSortedPages, _books) Using the IComparer Interface Let’s create another sample List sorted by Pages to verify this functionality: private static void CreateBookDataSortedByPages() NET/C# content and get paid? > JOIN US! Įlse if (this.Pages class to order the objects according to Pages property. Let’s modify the Book class to implement the IComparable interface: Greater than zero – meaning the compared instance comes before the current instance in the sort order.Zero – meaning both the compared and the current instances have the same position in the sort order.Less than zero – meaning the compared instance comes after the current instance in the sort order.This CompareTo() method returns an integer whose value can be: To do that, we need to implement the IComparable interface in the Book class and implement the CompareTo() method. The IComparable interface allows us to sort a list of complex types without using LINQ. Var sortedList = sort.SortByAuthorAndPagesUsingLinq(_books) ĬollectionAssert.AreEqual(_booksSortedAuthorPages, sortedList) Using the IComparable Interface We can verify this by executing the method: var sort = new Sort() This list contains Book objects sorted by multiple properties. Let’s create another sample sorted list: private static void CreateBookDataSortedByAuthorPages() This method returns a new List sorted, first by the Author property and then by the Pages property. Return originalList.OrderBy(x => x.Author).ThenBy(x => x.Pages).ToList() NET/C# content and get paid? > JOIN US! SortByAuthorAndPagesUsingLinq(List originalList) This method takes a List as argument and returns a new List that’s sorted according to the Title property. Return originalList.OrderBy(x => x.Title).ToList() Let’s look at a scenario to sort the List by the Title property by creating a method: public List SortByTitleUsingLinq(List originalList) This key should be a property of the object. This creates a new list with the elements sorted by using a key. The most common way of sorting lists is by using the OrderBy() LINQ method to create a new, sorted copy of the original list. Let’s create a CreateBookData() method to keep these Book objects in an unsorted List: private static void CreateBookData()Īnd, let’s create another CreateBookDataSortedByTitle() method to contain a sorted List by the Title property: private static void CreateBookDataSortedByTitle() Private static readonly Book emma = new Book Private static readonly Book pride = new Book Private static readonly Book lotr = new Book Private static readonly Book gatsby = new Book
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |