Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage OrderBy in LINQ firstly the Cyrillic words then the Latin words

Tags:

c#

.net

linq

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?

like image 521
Nikita Minaev Avatar asked Jan 03 '23 06:01

Nikita Minaev


2 Answers

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

like image 125
user1672994 Avatar answered Jan 14 '23 12:01

user1672994


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
like image 28
Jon Skeet Avatar answered Jan 14 '23 13:01

Jon Skeet