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

linq orderby multiple ascending descending
linq order by ascending
c# orderby descending
order by ascending in linq lambda c#
c# orderby string
c# orderby ascending
iqueryable orderby
linq orderby string alphabetical
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

orderby clause, In a query expression, the orderby clause causes the returned The sorting is performed by the default comparer for the type of the element In the following example, the first query sorts the words in alphabetical order starting from A, GetStudents() { // Use a collection initializer to create the data source. 7 Usage OrderBy in LINQ firstly the Cyrillic words then the Latin words Jun 20 '18 7 How to read .runsettings test parameter in xUnit fixture Mar 4 '19 6 View POST request body in Application Insights Dec 14 '18

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

Sorting Data (C#), The first sort criterion performs a primary sort on the elements. The standard query operator methods that sort data are listed in the Length select word; foreach (string str in query) Console. The next example demonstrates how to use the orderby descending clause in a LINQ query to sort the strings by  The intelligent algorithm for detecting Latin and Cyrillic alphabet has a need of minimal steps for transliteration, in the cases when the words from one language can have more than one meaning.

For the sake of alternative, if you don't want to implement a whole new custom order method, you can create your extension method and use existing order methods:

public static class MyExtensions
{
     public static IEnumerable<string> OrderByCyrillicFirst(this IEnumerable<string> list)
     {
         var cyrillicOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? false : IsCyrillic(l[0])).OrderBy(l => l);
         var latinOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? true : !IsCyrillic(l[0])).OrderBy(l => l);
         return cyrillicOrderedList.Concat(latinOrderedList);
     }

     public static IEnumerable<string> OrderByCyrillicFirstDescending(this IEnumerable<string> list)
     {
         var cyrillicOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? false : IsCyrillic(l[0])).OrderByDescending(l => l);
         var latinOrderedList = list.Where(l => string.IsNullOrEmpty(l) ? true : !IsCyrillic(l[0])).OrderByDescending(l => l);
         return cyrillicOrderedList.Concat(latinOrderedList);
     }

    //cyrillic symbols start with code 1024 and end with 1273.
    private static bool IsCyrillic(char ch) =>
        ch >= 1024 && ch <= 1273;       
}

and usage:

var listExample = new List<string>(){ "banana", "apple", "lemon", "orange", "cherry", "pear", "яблоко", "лимон", "груша", "банан", "апельсин", "вишня" };

var result = listExample.OrderByCyrillicFirst();

output:

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

References: DotNetFiddle example, Cyrillic Unicode Chart

OrderBy & OrderByDescending - Sorting Operators, This tutorial explains Sorting operators: OrderBy & OrderByDescending. A sorting LINQ includes following sorting operators. Used for second level sorting in ascending order. Use descending keyword to sort collection in descending order. First overload of OrderBy extension method accepts the Func delegate type  The version 3 text index is case insensitive for Latin characters with or without diacritics and characters from non-Latin alphabets, such as the Cyrillic alphabet. See text index for details. Earlier versions of the text index are case insensitive for Latin characters without diacritic marks; i.e. for [A-z].

Reverse and OrderBy - Using C# LINQ, These LINQ methods reorder the elements in an IEnumerable<T> sequence. IEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" };  Then, I would use String.Split and create a string array, maybe create a dictionary of the distinct words from the first array, then loop through the first loop and increment an integer value the corresponds to that word in the dictionary. When you are done looping then print out your Dictionary.

Sorting data: the OrderBy() & ThenBy() methods, Fortunately for us, LINQ has several easy-to-use methods for sorting data - let's try a basic example first: Download sample code. List<int> numbers = new  October 30, 2002 - 11:30 am UTC Reviewer: Sikandar Hayat Awan from Pindi Bhattian - Pakistan Hi TOM, On the server there is US7ASCII character set but I need 128,129,130 and 131 at client.

LINQ Ruined My Favorite Interview Question, Split(' ') group word by word into g let count = g.Count() orderby count descending select g. C# felt like someone explaining etymology by moving between Latin and It makes sense to use terms that many C# (and SQL) users are In your first post, everything is "linq is bad" in generic terms except for  If you are wondering, whether to read a text file, encoded in Cyrillic, Windows-1251 or UTF-8, then this question has no clear answer. Both standards are widely used for the recording of non-Latin text.

Comments
  • Do you only need this for LINQ to Objects (no EF etc)? How would you want to compare words that a part-Russian, part-English? (While "appleвишня" may not be a real word, it's certainly a valid string.)
  • Yes this need only for LINQ to Objects. I don't understand where such words Like "appleвишня" should come from.
  • Your code may never need to deal with them - but what do you want it to do if it ever does see them? Which should come first, "appleвишня" or "вишняapple"? Once you've decided that, it should be relatively easy to implement.
  • You can provide your own IComparer<TKey> implementation to OrderBy
  • It is really nonstandard but very interesting case. The first should be "вишняapple".