var listExample = new List<string>(){ "banana", "apple", "lemon", "orange",
"cherry", "pear", "яблоко", "лимон", "груша", "банан", "апельсин", "вишня" };
listExample = listExample.OrderBy(x => x).ToList();
The result is
{ "apple", "banana", "cherry", "lemon", "orange", "pear", "апельсин", "банан", "вишня", "груша", "лимон", "яблоко" }
But need first order by Russian then by English like this
{ "апельсин", "банан", "вишня", "груша", "лимон", "яблоко", "apple", "banana", "cherry", "lemon", "orange", "pear" }
How use OrderBy to get that result?
You can use the Order checking if the work is latin
.
The below code return the desired output where it's checking if the word is IsCyrillic.
var listExample = new List<string>(){ "banana", "apple", "lemon", "orange", "cherry",
"pear", "яблоко", "лимон", "груша", "банан", "апельсин", "вишня" };
var result = listExample.OrderBy(x => x)
.Select(x => new {val = x, isCyrillic = Regex.IsMatch(x, @"\p{IsCyrillic}")})
.OrderBy(x => !x.isCyrillic).Select(x => x.val);
foreach (var str in result)
{
Console.WriteLine(str);
}
Check the full code at this fiddle - https://dotnetfiddle.net/9o4FJt
The output of above program is
апельсин банан вишня груша лимон яблоко apple banana cherry lemon orange pear
For a fairly "quick and dirty" approach I'd probably order by "the first index containing a Cyrillic character" (using int.MaxValue
for "no Cyrillic") followed by regular ordering (which allows you to make it case-insensitive etc).
So something like:
var result = list.OrderBy(GetFirstCyrillicIndex).ThenBy(x => x).ToList();
...
private static int GetFirstCyrillicIndex(string text)
{
// This could be written using LINQ, but it's probably simpler this way.
for (int i = 0; i < text.Length; i++)
{
if (text[i] >= 0x400 && text[i] <= 0x4ff)
{
return i;
}
}
return int.MaxValue;
}
Complete example including my awkward words:
using System;
using System.Collections.Generic;
using System.Linq;
class Test
{
static void Main()
{
var list = new List<string> {
"banana", "apple", "lemon", "orange",
"cherry", "pear", "яблоко", "лимон",
"груша", "банан", "апельсин", "вишня",
"appleвишня", "вишняapple"
};
var result = list.OrderBy(GetFirstCyrillicIndex).ThenBy(x => x).ToList();
foreach (var item in result)
{
Console.WriteLine(item);
}
}
private static int GetFirstCyrillicIndex(string text)
{
// This could be written using LINQ, but it's probably simpler this way.
for (int i = 0; i < text.Length; i++)
{
if (text[i] >= 0x400 && text[i] <= 0x4ff)
{
return i;
}
}
return int.MaxValue;
}
}
Results:
апельсин
банан
вишня
вишняapple
груша
лимон
яблоко
appleвишня
apple
banana
cherry
lemon
orange
pear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With