List Sort With Comparison Delegate : List « Data Structure « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Collections.Generic;

using System.ComponentModel;

    public class Product
    {
        string name;
        public string Name
        {
            get { return name; }
        }

        decimal price;
        public decimal Price
        {
            get { return price; }
        }

        public Product(string name, decimal price)
        {
            this.name = name;
            this.price = price;
        }

        public static List<Product> GetSampleProducts()
        {
            List<Product> list = new List<Product>();
            list.Add(new Product("C", 9.99m));
            list.Add(new Product("A", 1.99m));
            list.Add(new Product("F", 2.99m));
            list.Add(new Product("S", 3.99m));
            return list;
        }

        public override string ToString()
        {
            return string.Format("{0}: {1}", name, price);
        }
    }

    class ListSortWithComparisonDelegate
    {
        static void Main()
        {
            List<Product> products = Product.GetSampleProducts();
            products.Sort(delegate(Product first, Product second)
                { return first.Name.CompareTo(second.Name); }
            );
            foreach (Product product in products)
            {
                Console.WriteLine(product);
            }
        }
    }








11.34.List
11.34.1.Obtaining a read-only copy of a list
11.34.2.Using the Action delegate
11.34.3.Converting a list from list of string to list of int
11.34.4.Converting a list: user defined converting function
11.34.5.Vector extends List
11.34.6.List Filtering With Linq
11.34.7.List Joining Ordering And Filtering With Linq
11.34.8.List Order With Extension Method
11.34.9.List Query With Delegates
11.34.10.List Query With Delegates Compact
11.34.11.List Query With Lambda Expression
11.34.12.List Sort With Comparer
11.34.13.List Sort With Comparison Delegate
11.34.14.Implement IComparable to use List.Sort
11.34.15.List.ForEach
11.34.16.Compact Code for looping through the List with delegate
11.34.17.List Convert All
11.34.18.Using external method to pass into Find method
11.34.19.List size and capacity
11.34.20.List range operation
11.34.21.Use Action<(Of <(T>)>) delegate to print the contents of a List<(Of <(T>)>) object.
11.34.22.Item property (the indexer in C#) and various other properties and methods of the List<(Of <(T>)>) generic class.