5

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.

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
classical312
  • 185
  • 1
  • 4
  • 11

3 Answers3

6

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
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
4

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;
}
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
2

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)

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142