Lexicographically sort C#
Asked Answered
C

3

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.

Citarella answered 1/3, 2016 at 15:47 Comment(3)
You can try: var sorted = e.OrderByDescending(x => x.Length).ThenBy(x => x).ToList()Taxable
I don't understand the pattern you want to achieve for the second case (5 input), mind to give the expected output?Mahound
Use this https://mcmap.net/q/1409115/-easiest-method-to-orderby-a-string-using-stringcomparison-ordinalExhaustion
M
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
Mahound answered 1/3, 2016 at 15:53 Comment(3)
I think you'll get exactly the same output if you leave out the .ThenBy().Amin
@MatthewWatson you are right! I just tested it. Seems like the non-char is lexically smaller than charMahound
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.Amin
P
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;
}
Personalty answered 1/3, 2016 at 15:54 Comment(1)
Is it possible for you to add how to sort just lexicographically?Rascal
L
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)

Looper answered 1/3, 2016 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.