您的位置:首页 > 其它

Linq101-Ordering

2014-12-30 16:32 141 查看
using System;
using System.Collections.Generic;
using System.Linq;

namespace Linq101
{
class Ordering
{
/// <summary>
/// This sample uses orderby to sort a list of words alphabetically.
/// </summary>
public void Linq28()
{
string[] words = { "cherry", "apple", "blueberry" };

var sortedWords = from w in words
orderby w
select w;

Console.WriteLine("The sorted list of words:");
foreach (var word in sortedWords)
{
Console.WriteLine(word);
}
}

/// <summary>
/// This sample uses orderby to sort a list of words by length.
/// </summary>
public void Linq29()
{
string[] words = { "cherry", "apple", "blueberry" };

var sortedWords = from w in words
orderby w.Length
select w;

Console.WriteLine("The sorted list of words( by length ):");
foreach (var word in sortedWords)
{
Console.WriteLine(word);
}
}

/// <summary>
/// This sample uses orderby to sort a list of products by name.
/// </summary>
public void Linq30()
{
var products = Data.GetProductList();

var query = from p in products
orderby p.ProductName
select p;

ObjectDumper.Write(query);
}

/// <summary>
/// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array.
/// </summary>
public void Linq31()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };

var query = words.OrderBy(w => w, new CaseInsensitiveComparer());

ObjectDumper.Write(query);
}

private class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase);
}
}

/// <summary>
/// This sample uses orderby and descending to sort a list of doubles from highest to lowest.
/// </summary>
public void Linq32()
{
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

var sortedDoubles = from d in doubles
orderby d descending
select d;

ObjectDumper.Write(sortedDoubles);
}

/// <summary>
/// This sample uses orderby to sort a list of products by units in stock from highest to lowest.
/// </summary>
public void Linq33()
{
var products = Data.GetProductList();

var query = from p in products
orderby p.UnitsInStock descending
select p;

ObjectDumper.Write(query);
}

/// <summary>
/// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array.
/// </summary>
public void Linq34()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };

var query = words.OrderByDescending(w => w, new CaseInsensitiveComparer());

ObjectDumper.Write(query);
}

/// <summary>
/// This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself.
/// </summary>
public void Linq35()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var sortedDigits = from d in digits
orderby d.Length, d
select d;

Console.WriteLine("Sorted digits:");
foreach (var d in sortedDigits)
{
Console.WriteLine(d);
}
}

/// <summary>
/// This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array.
/// </summary>
public void Linq36()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };

var sortedWords = words
.OrderBy(w => w.Length)
.ThenBy(w => w, new CaseInsensitiveComparer());

ObjectDumper.Write(sortedWords);
}

/// <summary>
/// This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest.
/// </summary>
public void Linq37()
{
var products = Data.GetProductList();

var query = from p in products
orderby p.Category, p.UnitPrice descending
select p;

ObjectDumper.Write(query);
}

/// <summary>
/// This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive descending sort of the words in an array.
/// </summary>
public void Linq38()
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };

var sortedWords = words.OrderBy(w => w.Length).ThenByDescending(w => w, new CaseInsensitiveComparer());

ObjectDumper.Write(sortedWords);
}

/// <summary>
/// This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array.
/// </summary>
public void Linq39()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

var sortedDigits = (from d in digits
where d[1] == 'i'
select d)
.Reverse();

ObjectDumper.Write(sortedDigits);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: