- #region Ordering Operator
- /* OrderBy 第一种原型
- * K必须实现Icomparable接口
- * public static IOrderedEnumerable<T> OrderBy<T, K>(
- * this IEnumerable<T> source,
- * Func<T, K> keySelector)
- * where
- * K : IComparable<K>;
- *
- *
- *
- */
- static void OrderByFirstPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- IEnumerable<string> items = presidents.OrderBy(p => p.Length);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*OrderBy 第二种原型
- *
- * public static IOrderedEnumerable<T> OrderBy<T, K>(
- * this IEnumerable<T> source,
- * Func<T, K> keySelector,
- * IComparer<K> comparer);
- */
- static void OrderBySecondPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
- IEnumerable<string> items = presidents.OrderBy(s => s, comparer);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*OrderByDescending 第一种原型
- * K必须实现Icomparable接口
- * public static IOrderedEnumerable<T> OrderByDescending<T, K>(
- * this IEnumerable<T> source,
- * Func<T, K> keySelector)
- * where
- * K : IComparable<K>;
- *
- *
- */
- static void OrderByDescendingPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- IEnumerable<string> items = presidents.OrderByDescending(p => p.Length);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*OrderByDescending 第二种原型
- *
- * public static IOrderedEnumerable<T> OrderByDescending<T, K>(
- * this IEnumerable<T> source,
- * Func<T, K> keySelector,
- * IComparer<K> comparer);
- *
- *
- */
- static void OrderByDescendingSecondPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
- IEnumerable<string> items = presidents.OrderByDescending(s => s, comparer);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*ThenBy 第一种原型
- *
- * K必须实现 IComparable
- * public static IOrderedEnumerable<T> ThenBy<T, K>(
- * this IOrderedEnumerable<T> source,
- * Func<T, K> keySelector)
- * where
- * K : IComparable<K>;
- *
- */
- static void ThenByFirstPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- IEnumerable<string> items = presidents.OrderBy(p => p.Length).ThenBy(s=>s);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*ThenBy 第二种原型
- *
- * public static IOrderedEnumerable<T> ThenBy<T, K>(
- * this IOrderedEnumerable<T> source,
- * Func<T, K> keySelector,
- * IComparer<K> comparer);
- *
- */
- static void ThenBySecondPrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- MyVowelConsonantRatioComparer comparer = new MyVowelConsonantRatioComparer();
- IEnumerable<string> items = presidents.OrderBy(s => s, comparer).ThenBy(s=>s);
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- /*ThenByDescending 第一种原型 用法和ThenBy相同
- * public static IOrderedEnumerable<T> ThenByDescending<T, K>(
- * this IOrderedEnumerable<T> source,
- * Func<T, K> keySelector)
- * where
- * K : IComparable<K>;
- */
- /*ThenByDescending 第二种原型 用法和ThenBy相同
- * public static IOrderedEnumerable<T> ThenByDescending<T, K>(
- * this IOrderedEnumerable<T> source,
- * Func<T, K> keySelector,
- * IComparer<K> comparer);
- *
- */
- /* Reverse 原型
- *
- *public static IEnumerable<T> Reverse<T>(
- * this IEnumerable<T> source);
- *
- */
- static void ReversePrototype()
- {
- string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Aarter", "Cleveland" };
- IEnumerable<string> items = presidents.Reverse();
- foreach (string item in items)
- {
- Console.WriteLine(item);
- }
- }
- #endregion
- //--------------------------------MyVowelConsonantRatioComparer class ---------------------------
- ///<summary>
- /// for OderBy Second Protytype IComparer
- ///</summary>
- public class MyVowelConsonantRatioComparer :IComparer<string>
- {
- public int Compare(string s1, string s2)
- {
- int vCount1 = 0;
- int cCount1 = 0;
- int vCount2 = 0;
- int cCount2 = 0;
- GetVowelConsonantCount(s1, ref vCount1, ref cCount1);
- GetVowelConsonantCount(s2, ref vCount2, ref cCount2);
- double dRatio1 = (double)vCount1 / cCount1;
- double dRatio2 = (double)vCount2 / cCount2;
- if (dRatio1 < dRatio2)
- {
- return -1;
- }
- else if (dRatio1 == dRatio2)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- public void GetVowelConsonantCount(string s, ref int vowelCount, ref int consonantCount)
- {
- //元音
- string vowels = "AEIOY";
- vowelCount = 0;//元音数量
- consonantCount = 0;//辅音数量
- string sUpper = s.ToUpper();
- foreach (char ch in sUpper)
- {
- if (vowels.IndexOf(ch) < 0)
- {
- consonantCount++;
- }
- else
- {
- vowelCount++;
- }
- }
- }
- }
Linq Ordering Operator
最新推荐文章于 2024-10-17 20:59:27 发布