在 LINQ 中使用 OrderBy 首先是西里尔字母,然后是拉丁词

var listExample = new List<string>(){ "banana", "apple", "lemon", "orange", 

"cherry", "pear", "яблоко", "лимон", "груша", "банан", "апельсин", "вишня" };

listExample = listExample.OrderBy(x => x).ToList();

结果是


{ "apple", "banana", "cherry", "lemon", "orange", "pear", "апельсин", "банан", "вишня", "груша", "лимон", "яблоко" }

但是需要先用俄语然后像这样用英语


{ "апельсин", "банан", "вишня", "груша", "лимон", "яблоко", "apple", "banana", "cherry", "lemon", "orange", "pear" }

如何使用 OrderBy 获得该结果?


幕布斯6054654
浏览 171回答 3
3回答

万千封印

作为替代方案,如果您不想实现全新的自定义订单方法,您可以创建扩展方法并使用现有的订单方法:public static class MyExtensions{&nbsp; &nbsp; &nbsp;public static IEnumerable<string> OrderByCyrillicFirst(this IEnumerable<string> list)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var cyrillicOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? false : IsCyrillic(l[0])).OrderBy(l => l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var latinOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? true : !IsCyrillic(l[0])).OrderBy(l => l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return cyrillicOrderedList.Concat(latinOrderedList);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;public static IEnumerable<string> OrderByCyrillicFirstDescending(this IEnumerable<string> list)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var cyrillicOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? false : IsCyrillic(l[0])).OrderByDescending(l => l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var latinOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? true : !IsCyrillic(l[0])).OrderByDescending(l => l);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return cyrillicOrderedList.Concat(latinOrderedList);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; //cyrillic symbols start with code 1024 and end with 1273.&nbsp; &nbsp; private static bool IsCyrillic(char ch) =>&nbsp; &nbsp; &nbsp; &nbsp; ch >= 1024 && ch <= 1273;&nbsp; &nbsp; &nbsp; &nbsp;}和用法:var listExample = new List<string>(){ "banana", "apple", "lemon", "orange", "cherry", "pear", "яблоко", "лимон", "груша", "банан", "апельсин", "вишня" };var result = listExample.OrderByCyrillicFirst();输出:апельсин банан вишня груша лимон яблоко 苹果香蕉樱桃柠檬橙梨

翻阅古今

对于相当“快速和肮脏”的方法,我可能会按“包含西里尔字母的第一个索引”(int.MaxValue用于“无西里尔字母”)进行排序,然后是常规排序(允许您使其不区分大小写等)。所以像:var result = list.OrderBy(GetFirstCyrillicIndex).ThenBy(x => x).ToList();...private static int GetFirstCyrillicIndex(string text){&nbsp; &nbsp; // This could be written using LINQ, but it's probably simpler this way.&nbsp; &nbsp; for (int i = 0; i < text.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (text[i] >= 0x400 && text[i] <= 0x4ff)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return int.MaxValue;}完整的例子,包括我尴尬的话:using System;using System.Collections.Generic;using System.Linq;class Test{&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var list = new List<string> {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "banana", "apple", "lemon", "orange",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "cherry", "pear", "яблоко", "лимон",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "груша", "банан", "апельсин", "вишня",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "appleвишня", "вишняapple"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var result = list.OrderBy(GetFirstCyrillicIndex).ThenBy(x => x).ToList();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in result)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static int GetFirstCyrillicIndex(string text)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // This could be written using LINQ, but it's probably simpler this way.&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < text.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (text[i] >= 0x400 && text[i] <= 0x4ff)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return int.MaxValue;&nbsp; &nbsp; }}结果:апельсинбананвишнявишняappleгрушалимоняблокоappleвишняapplebananacherrylemonorangepear
打开App,查看更多内容
随时随地看视频慕课网APP