Linq Ordering Operator

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值