Lexicographically sort C#

14,689

Solution 1

Edit:

By default, the non-char is lexically smaller than the char, thus, you can exploit this and omit ThenBy but will still get the same result like this (Credit goes to Matthew Watson):

string str = "abba ebacaba bcd er abacaba output ebacabaabacabaabbabcder";
string[] strs = str.Split(' ').OrderBy(x => x).ToArray(); //same result, but shorter

Original:

Use OrderBy and also ThenBy

string str = "abba ebacaba bcd er abacaba output ebacabaabacabaabbabcder";
string[] strs = str.Split(' ').OrderBy(x => x).ThenBy(x => x.Length).ToArray();

You will get:

abacaba //aba is earlier than abb
abba
bcd
ebacaba
ebacabaabacabaabbabcder
er

Solution 2

You can use thenby :

static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
    // Use LINQ to sort the array received and return a copy.
    var sorted = e.OrderByDescending(s=>s.Length).ThenBy(r=>r);                 
    return sorted;
}

Solution 3

Change your function t:

static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
        // Use LINQ to sort the array received and return a copy.
        var sorted = from s in e
                     orderby s.Length descending, s
                     select s;
        return sorted;
    }

Which will output:

abacabaabbabcder
abacaba
output
abb
bcd
edr

When used with new List<string>{"abb", "abacaba", "bcd", "edr", "output", "abacabaabbabcder"}

Because it will order by s.Length, then by s (lexical order)

Share:
14,689
classical312
Author by

classical312

Updated on June 05, 2022

Comments

  • classical312
    classical312 almost 2 years

    I have this code for sorting strings:

     class Program
    {
        static void Main()
        {
    
            int x = Convert.ToInt32(Console.ReadLine());
            List<string> sampleList = new List<string>();
    
            for (int i=0; i<x; i++)
            {
                string word = Console.ReadLine();
                sampleList.Add(word);
            }
    
    
            foreach (string s in SortByLength(sampleList))
            {
                Console.Write(s);
            }
            Console.ReadLine();
        }
    
        static IEnumerable<string> SortByLength(IEnumerable<string> e)
        {
            // Use LINQ to sort the array received and return a copy.
            var sorted = from s in e
                         orderby s.Length descending
                         select s;
            return sorted;
        }
    }
    

    This code sorting strings by length, how can I do that by length and lexicographically ?

    Example

    //Input
    4
    abba
    abacaba
    bcd
    er
    
    //Output
    abacabaabbabcder
    

    In this case work fine, but when I have

    //Input
    5
    abba
    ebacaba
    bcd
    er
    abacaba
    
    //Output
    ebacabaabacabaabbabcder
    

    My first string is ebacaba which is wrong.

    • Teodor Kurtev
      Teodor Kurtev about 8 years
      You can try: var sorted = e.OrderByDescending(x => x.Length).ThenBy(x => x).ToList()
    • Ian
      Ian about 8 years
      I don't understand the pattern you want to achieve for the second case (5 input), mind to give the expected output?
    • Ahmad Hajou
      Ahmad Hajou over 2 years
  • Matthew Watson
    Matthew Watson about 8 years
    I think you'll get exactly the same output if you leave out the .ThenBy().
  • Ian
    Ian about 8 years
    @MatthewWatson you are right! I just tested it. Seems like the non-char is lexically smaller than char
  • Matthew Watson
    Matthew Watson about 8 years
    It's standard dictionary order, after all. Makes me wonder what the OP wants... Ah! Just realised! He wants to sort by length and THEN by value.
  • Teddy Codes
    Teddy Codes over 7 years
    Is it possible for you to add how to sort just lexicographically?