Linq Order操作

 Linq Ordering Operator

简单例子:
  1.          #region Ordering Operator
  2.  
  3.  
  4.         /* OrderBy 第一种原型
  5.          * K必须实现Icomparable接口
  6.          * public static IOrderedEnumerable<T> OrderBy<T, K>(
  7.          *       this IEnumerable<T> source,
  8.          *       Func<T, K> keySelector)
  9.          *           where
  10.          *               K : IComparable<K>;
  11.          *
  12.          *
  13.          *
  14.          */
  15.         static void OrderByFirstPrototype()
  16.         {
  17.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  18.             IEnumerable<string> items = presidents.OrderBy(p => p.Length);
  19.             foreach (string item in items)
  20.             {
  21.                 Console.WriteLine(item);
  22.             }
  23.         }
  24.  
  25.         /*OrderBy 第二种原型
  26.          *
  27.          * public static IOrderedEnumerable<T> OrderBy<T, K>(
  28.          *                      this IEnumerable<T> source,
  29.          *                        Func<T, K> keySelector,
  30.          *                       IComparer<K> comparer);
  31.          */
  32.         static void OrderBySecondPrototype()
  33.         {
  34.        
  35.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  36.             MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
  37.             IEnumerable<string> items = presidents.OrderBy(s => s, comparer);
  38.             foreach (string item in items)
  39.             {
  40.                 Console.WriteLine(item);
  41.             }
  42.  
  43.         }
  44.  
  45.         /*OrderByDescending 第一种原型
  46.          * K必须实现Icomparable接口
  47.          * public static IOrderedEnumerable<T> OrderByDescending<T, K>(
  48.          *       this IEnumerable<T> source,
  49.          *        Func<T, K> keySelector)
  50.          *        where
  51.          *        K : IComparable<K>;
  52.          *
  53.          *
  54.          */
  55.         static void OrderByDescendingPrototype()
  56.         {
  57.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  58.             IEnumerable<string> items = presidents.OrderByDescending(p => p.Length);
  59.             foreach (string item in items)
  60.             {
  61.                 Console.WriteLine(item);
  62.             }
  63.         }
  64.         /*OrderByDescending 第二种原型
  65.          *
  66.          * public static IOrderedEnumerable<T> OrderByDescending<T, K>(
  67.          *    this IEnumerable<T> source,
  68.          *          Func<T, K> keySelector,
  69.          *                IComparer<K> comparer);
  70.          *
  71.          *
  72.          */
  73.         static void OrderByDescendingSecondPrototype()
  74.         {
  75.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  76.             MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
  77.             IEnumerable<string> items = presidents.OrderByDescending(s => s, comparer);
  78.             foreach (string item in items)
  79.             {
  80.                 Console.WriteLine(item);
  81.             }
  82.         }
  83.  
  84.         /*ThenBy 第一种原型
  85.          *
  86.          * K必须实现 IComparable
  87.          * public static IOrderedEnumerable<T> ThenBy<T, K>(
  88.          *           this IOrderedEnumerable<T> source,
  89.          *           Func<T, K> keySelector)
  90.          *              where
  91.          *               K : IComparable<K>;
  92.          *
  93.          */
  94.         static void ThenByFirstPrototype()
  95.         {
  96.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  97.             IEnumerable<string> items = presidents.OrderBy(p => p.Length).ThenBy(s=>s);
  98.             foreach (string item in items)
  99.             {
  100.                 Console.WriteLine(item);
  101.             }
  102.         }
  103.  
  104.         /*ThenBy 第二种原型
  105.          *
  106.          * public static IOrderedEnumerable<T> ThenBy<T, K>(
  107.          *       this IOrderedEnumerable<T> source,
  108.          *       Func<T, K> keySelector,
  109.          *       IComparer<K> comparer);
  110.          *
  111.          */
  112.         static void ThenBySecondPrototype()
  113.         {
  114.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  115.             MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
  116.             IEnumerable<string> items = presidents.OrderBy(s => s, comparer).ThenBy(s=>s);
  117.             foreach (string item in items)
  118.             {
  119.                 Console.WriteLine(item);
  120.             }
  121.         }
  122.  
  123.         /*ThenByDescending 第一种原型 用法和ThenBy相同
  124.          * public static IOrderedEnumerable<T> ThenByDescending<T, K>(
  125.          *           this IOrderedEnumerable<T> source,
  126.          *           Func<T, K> keySelector)
  127.          *           where
  128.          *           K : IComparable<K>;
  129.          */
  130.  
  131.         /*ThenByDescending 第二种原型 用法和ThenBy相同
  132.          * public static IOrderedEnumerable<T> ThenByDescending<T, K>(
  133.          *        this IOrderedEnumerable<T> source,
  134.          *        Func<T, K> keySelector,
  135.          *        IComparer<K> comparer);
  136.          *
  137.          */
  138.  
  139.  
  140.         /* Reverse 原型
  141.          *
  142.          *public static IEnumerable<T> Reverse<T>(
  143.          *       this IEnumerable<T> source);
  144.          *
  145.          */
  146.         static void ReversePrototype()
  147.         {
  148.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Aarter""Cleveland" };
  149.             IEnumerable<string> items = presidents.Reverse();
  150.             foreach (string item in items)
  151.             {
  152.                 Console.WriteLine(item);
  153.             }
  154.         }
  155.         #endregion
  156.  
  157.  //--------------------------------MyVowelConsonantRatioComparer  class ---------------------------
  158. ///<summary>
  159.     /// for OderBy Second Protytype IComparer
  160.     ///</summary>
  161.    public class MyVowelConsonantRatioComparer :IComparer<string>
  162.    {
  163.        public int Compare(string s1, string s2)
  164.        {
  165.            int vCount1 = 0;
  166.            int cCount1 = 0;
  167.            int vCount2 = 0;
  168.            int cCount2 = 0;
  169.  
  170.            GetVowelConsonantCount(s1, ref vCount1, ref cCount1);
  171.            GetVowelConsonantCount(s2, ref vCount2, ref cCount2);
  172.  
  173.            double dRatio1 = (double)vCount1 / cCount1;
  174.            double dRatio2 = (double)vCount2 / cCount2;
  175.  
  176.            if (dRatio1 < dRatio2)
  177.            {
  178.                return -1;
  179.            }
  180.            else if (dRatio1 == dRatio2)
  181.            {
  182.                return 0;
  183.            }
  184.            else
  185.            {
  186.                return 1;
  187.            }
  188.        }
  189.        public void GetVowelConsonantCount(string s, ref int vowelCount, ref int consonantCount)
  190.        {
  191.            //元音
  192.            string vowels = "AEIOY";
  193.            vowelCount = 0;//元音数量
  194.            consonantCount = 0;//辅音数量
  195.  
  196.            string sUpper = s.ToUpper();
  197.  
  198.            foreach (char ch in sUpper)
  199.            {
  200.                if (vowels.IndexOf(ch) < 0)
  201.                {
  202.                    consonantCount++;
  203.                }
  204.                else
  205.                {
  206.                    vowelCount++;
  207.                }
  208.            }
  209.  
  210.        }
  211.    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值