• Linq101-Ordering


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 
      5 namespace Linq101
      6 {
      7     class Ordering
      8     {
      9         /// <summary>
     10         /// This sample uses orderby to sort a list of words alphabetically.
     11         /// </summary>
     12         public void Linq28()
     13         {
     14             string[] words = { "cherry", "apple", "blueberry" };
     15 
     16             var sortedWords = from w in words
     17                               orderby w
     18                               select w;
     19 
     20             Console.WriteLine("The sorted list of words:");
     21             foreach (var word in sortedWords)
     22             {
     23                 Console.WriteLine(word);
     24             }
     25         }
     26 
     27         /// <summary>
     28         /// This sample uses orderby to sort a list of words by length.
     29         /// </summary>
     30         public void Linq29()
     31         {
     32             string[] words = { "cherry", "apple", "blueberry" };
     33 
     34             var sortedWords = from w in words
     35                               orderby w.Length
     36                               select w;
     37 
     38             Console.WriteLine("The sorted list of words( by length ):");
     39             foreach (var word in sortedWords)
     40             {
     41                 Console.WriteLine(word);
     42             }
     43         }
     44 
     45         /// <summary>
     46         /// This sample uses orderby to sort a list of products by name.
     47         /// </summary>
     48         public void Linq30()
     49         {
     50             var products = Data.GetProductList();
     51 
     52             var query = from p in products
     53                         orderby p.ProductName
     54                         select p;
     55 
     56             ObjectDumper.Write(query);
     57         }
     58 
     59         /// <summary>
     60         /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array.
     61         /// </summary>
     62         public void Linq31()
     63         {
     64             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
     65 
     66             var query = words.OrderBy(w => w, new CaseInsensitiveComparer());
     67 
     68             ObjectDumper.Write(query);
     69         }
     70 
     71         private class CaseInsensitiveComparer : IComparer<string>
     72         {
     73             public int Compare(string x, string y)
     74             {
     75                 return string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase);
     76             }
     77         }
     78 
     79         /// <summary>
     80         /// This sample uses orderby and descending to sort a list of doubles from highest to lowest.
     81         /// </summary>
     82         public void Linq32()
     83         {
     84             double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
     85 
     86             var sortedDoubles = from d in doubles
     87                                 orderby d descending
     88                                 select d;
     89 
     90             ObjectDumper.Write(sortedDoubles);
     91         }
     92 
     93         /// <summary>
     94         /// This sample uses orderby to sort a list of products by units in stock from highest to lowest.
     95         /// </summary>
     96         public void Linq33()
     97         {
     98             var products = Data.GetProductList();
     99 
    100             var query = from p in products
    101                         orderby p.UnitsInStock descending
    102                         select p;
    103 
    104             ObjectDumper.Write(query);
    105         }
    106 
    107         /// <summary>
    108         /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array.
    109         /// </summary>
    110         public void Linq34()
    111         {
    112             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
    113 
    114             var query = words.OrderByDescending(w => w, new CaseInsensitiveComparer());
    115 
    116             ObjectDumper.Write(query);
    117         }
    118 
    119         /// <summary>
    120         /// 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.
    121         /// </summary>
    122         public void Linq35()
    123         {
    124             string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    125 
    126             var sortedDigits = from d in digits
    127                                orderby d.Length, d
    128                                select d;
    129 
    130             Console.WriteLine("Sorted digits:");
    131             foreach (var d in sortedDigits)
    132             {
    133                 Console.WriteLine(d);
    134             }
    135         }
    136 
    137         /// <summary>
    138         /// 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.
    139         /// </summary>
    140         public void Linq36()
    141         {
    142             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
    143 
    144             var sortedWords = words
    145                 .OrderBy(w => w.Length)
    146                 .ThenBy(w => w, new CaseInsensitiveComparer());
    147 
    148             ObjectDumper.Write(sortedWords);
    149         }
    150 
    151         /// <summary>
    152         /// This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest.
    153         /// </summary>
    154         public void Linq37()
    155         {
    156             var products = Data.GetProductList();
    157 
    158             var query = from p in products
    159                         orderby p.Category, p.UnitPrice descending
    160                         select p;
    161 
    162             ObjectDumper.Write(query);
    163         }
    164 
    165         /// <summary>
    166         /// 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.
    167         /// </summary>
    168         public void Linq38()
    169         {
    170             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
    171 
    172             var sortedWords = words.OrderBy(w => w.Length).ThenByDescending(w => w, new CaseInsensitiveComparer());
    173 
    174             ObjectDumper.Write(sortedWords);
    175         }
    176 
    177         /// <summary>
    178         /// 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.
    179         /// </summary>
    180         public void Linq39()
    181         {
    182             string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    183 
    184             var sortedDigits = (from d in digits
    185                                 where d[1] == 'i'
    186                                 select d)
    187                                 .Reverse();
    188 
    189             ObjectDumper.Write(sortedDigits);
    190         }
    191     }
    192 }
  • 相关阅读:
    内存映射mmap的几个api及其使用
    hiredis的安装
    Linux 下解压大全
    redis内存数据库C客户端hiredis API 中文说明
    C/C++使用MySQL
    搜索引擎的缓存(cache)机制
    快速排序(QuickSort)
    冒泡排序
    spring核心之AOP学习总结一
    Spring学习总结六——SpringMVC一
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4193961.html
Copyright © 2020-2023  润新知