views:

837

answers:

7

By alphabetically by length I mean as follows:

given: { "=", "==>>", "=>>", "=>", "!>" }

I want to get out:

!>
=
=>
=>>
==>>

I'm currently just using OrderBy(x => x.Length).ToArray()

anyone got a better lambda?

EDIT: I'm going about this ALL wrong, please vote to close!

+5  A: 

I don't think the sorting you're trying to do is any different than a standard dictionary sort, unless I'm missing something.

Josh Kodroff
it seems to be :-/
Firoso
I think i screwed up... better real world example in post of what I want to do.
Firoso
Yeah I wanted it the OTHER way round.
Firoso
+3  A: 

"Alphabetically by length" doesn't mean anything. You can sort alphabetically, or you can sort by length. Or, you can sort by one and then sort by the other when the first sort says that they're the same. In your example, you seem to be sorting exclusively alphabetically.

Aric TenEyck
A: 

I'm not totally clear about your question, but it seems you want to sort firstly by string length, then by alphabetical position.

You could try this as a LINQ solution:

vals = vals.OrderBy(str => str.Length).ThenBy(str => str).ToArray();

If you want a (rather more efficient) solution that does not use LINQ (though still makes nice use of lambda expressions), you could try the following:

Array.Sort(vals, (strA, strB) =>
{
    var lengthComp = Comparer<int>.Default.Compare(strA.Length, strB.Length);
    if (lengthComp == 0)
        return string.Compare(strA, strB);
    return lengthComp;
});

Hope that helps.

Noldorin
A: 

Using a standard string compare function for sorting should do exactly that.

strncmp() in C

String.compare() in C++

String.compareTo() in Java

all work for that purpose.

Graphics Noob
Well done for not only *not* answering the question but also providing examples in every mainstream C-style language *except* C#.
Noldorin
A: 

I tried OrderBy(x => x) with the test data that you provided, and that produces exactly the output that you want.

If it's an array that you have from start, you can just use the Array.Sort method:

Array.Sort(data);

Edit:
The above does not apply to the new data that you have put in the question. Awaiting a clarification about the data (as commented to the question).

Guffa
A: 

If you want to sort by Alpha then by length, use the OrderBy and ThenBy methods:

var strings = new List<string> { "=", "==>>", "=>>", "=>", "!>" };
strings = strings.OrderBy( x => x ).ThenBy( x => x.Length ).ToList();
strings.ForEach( Console.WriteLine );
Metro Smurf
+4  A: 

Writing your own string comparer you can do this, which meets both set of requirements you've posted.

    public class MyStringComparer : IComparer<string>
    {
        #region IComparer<string> Members

        public int Compare(string x, string y)
        {
            if (x[0] == y[0])
            {
                return x.Length.CompareTo(y.Length);
            }
            else return x[0].CompareTo(y[0]);
        }

        #endregion
    }

a bcde bcd b bced cb

would yield:

a b bcd bcde bced cb

and { "=", "==>>", "=>>", "=>", "!>" }

yields:

!>
=
=>
=>>
==>>

by calling: myStringCollection.OrderBy(x=>x, new MyStringComparer).ToArray();

Timothy Carter
I don't see the point of creating a custom class here when lambda expressions can do the same thing more succinctly, as I demonstrate.
Noldorin