c sort list example
- 2020-05-30 20:57:56
- OfStack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListSort
{
class Program
{
static void Main(string[] args)
{
List listCustomer = new List();
listCustomer.Add(new Customer { name = " The customer 1", id = 0 });
listCustomer.Add(new Customer { name = " The customer 2", id = 1 });
listCustomer.Add(new Customer { name = " The customer 3", id = 5 });
listCustomer.Add(new Customer { name = " The customer 4", id = 3 });
listCustomer.Add(new Customer { name = " The customer 5", id = 4 });
listCustomer.Add(new Customer { name = " The customer 6", id = 5 });
/// ascending
List listCustomer1 = listCustomer.OrderBy(s => s.id).ToList();
// Descending order
List listCustomer2 = listCustomer.OrderByDescending(s => s.id).ToList();
//Linq The sorting way
List listCustomer3 = (from c in listCustomer
orderby c.id descending //ascending
select c).ToList();
Console.WriteLine("List.OrderBy Method sort in ascending order ");
foreach (Customer customer in listCustomer1)
{
Console.WriteLine(customer.name);
}
Console.WriteLine("List.OrderByDescending Method sort in descending order ");
foreach (Customer customer in listCustomer2)
{
Console.WriteLine(customer.name);
}
Console.WriteLine("Linq Method sort in descending order ");
foreach (Customer customer in listCustomer3)
{
Console.WriteLine(customer.name);
}
Console.ReadKey();
}
}
class Customer
{
public int id { get; set; }
public string name { get; set; }
}
}