12.4-Lambda Expressions
12.4-Lambda Expressions
4-Lambda expressions
Program
using System;
using System.Collections.Generic;
class Product
class Program
};
DisplayProducts(products);
// Sorting products by name using selection sort
SelectionSortByName(products);
DisplayProducts(products);
SelectionSortByPrice(products);
DisplayProducts(products);
Console.WriteLine();
int minIndex = i;
if (string.Compare(products[j].Name, products[minIndex].Name,
StringComparison.OrdinalIgnoreCase) < 0)
minIndex = j;
if (minIndex != i)
products[i] = products[minIndex];
products[minIndex] = temp;
int maxIndex = i;
maxIndex = j;
}
}
if (maxIndex != i)
products[i] = products[maxIndex];
products[maxIndex] = temp;
Exercise
Write a C# program that manages a list of products with their respective IDs, names, and prices.
It demonstrates sorting products by name and price in descending order using selection sort
algorithms.
Sorts the products by price in descending order using the SelectionSortByPrice method.
Sorts the list of products by name using the selection sort algorithm.
Compares product prices and swaps products accordingly to arrange them in descending order of
price.
Selection sort repeatedly selects the minimum (or maximum) element from the unsorted portion of
Hint
Analyze the SelectionSortByName method, which implements the selection sort algorithm to sort the
Pay attention to the comparison logic used to compare product names and the swapping
Examine the SelectionSortByPrice method, which sorts the list of products by price in descending
Understand how product prices are compared and products are swapped to arrange them in
Explanation
Program sorting a list of products by name and price using the selection sort algorithm. Let's break
The program begins by defining a Product class with properties Id, Name, and Price. It also contains
the Program class with the Main method serving as the entry point. Inside Main, a list of Product
objects is created and initialized with sample data representing different products. The original list of
The program then sorts the products by name using the SelectionSortByName method, which
implements the selection sort algorithm. This algorithm iterates through the list, finding the minimum
element by comparing product names, and swaps elements accordingly. After sorting by name, the
Similarly, the program sorts the products by price using the SelectionSortByPrice method, which
applies the selection sort algorithm but compares product prices instead. The sorted list of products