Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - Order by number then letters

Tags:

c#

linq

I have a list of strings, and these strings contain numbers and words.

What I wanted to do is order it by the numbers (numeric order) followed by the words (alphabetical order)

My list does not contain a mix of the two... here is an example

1, 5, 500 , LT, RT, 400 -> LINQ -> 1, 5, 400, 500, LT, RT

Here is a example of what I have, it works but I was wondering if there is a better way of writing it?

            int results = 0;
            // Grabs all voltages
            var voltage = ActiveRecordLinq.AsQueryable<Equipment>()
                .OrderBy(x => x.Voltage)
                .Select(x => x.Voltage)
                .Distinct()
                .ToList();
            // Order by numeric
            var numberVoltage = voltage
                .Where( x => int.TryParse(x, out results))
                .OrderBy( x => Convert.ToInt32(x));
            // Then by alpha
            var letterVoltage = voltage
                .Where(x=> !String.IsNullOrEmpty(x))
                .Where(x => !int.TryParse(x, out results))
                .OrderBy(x => x);

            return numberVoltage.Union(letterVoltage)

Thanks for the help!

like image 700
ChickSentMeHighE Avatar asked Feb 25 '11 18:02

ChickSentMeHighE


2 Answers

Given that you're doing it all in-process (as you've got a ToList call) I think I'd just use a custom comparer:

return ActiveRecordLinq.AsQueryable<Equipment>()
                       .Select(x => x.Voltage)
                       .Distinct()
                       .AsEnumerable() // Do the rest in-process
                       .Where(x => !string.IsNullOrEmpty(x))
                       .OrderBy(x => x, new AlphaNumericComparer())
                       .ToList();

Where AlphaNumericComparer implements IComparer<string>, something like this:

public int Compare(string first, string second)
{
    // For simplicity, let's assume neither is null :)

    int firstNumber, secondNumber;
    bool firstIsNumber = int.TryParse(first, out firstNumber);
    bool secondIsNumber = int.TryParse(second, out secondNumber);

    if (firstIsNumber)
    {
        // If they're both numbers, compare them; otherwise first comes first
        return secondIsNumber ? firstNumber.CompareTo(secondNumber) : -1;
    }
    // If second is a number, that should come first; otherwise compare
    // as strings
    return secondIsNumber ? 1 : first.CompareTo(second);
}

You could use a giant conditional for the latter part:

public int Compare(string first, string second)
{
    // For simplicity, let's assume neither is null :)

    int firstNumber, secondNumber;
    bool firstIsNumber = int.TryParse(first, out firstNumber);
    bool secondIsNumber = int.TryParse(second, out secondNumber);

    return firstIsNumber 
        ? secondIsNumber ? firstNumber.CompareTo(secondNumber) : -1;
        : secondIsNumber ? 1 : first.CompareTo(second);
}

... but in this case I don't think I would :)

like image 74
Jon Skeet Avatar answered Sep 28 '22 00:09

Jon Skeet


This solution attempts parsing once for each value.

List<string> voltage = new List<string>() { "1", "5", "500" , "LT", "RT", "400" };

List<string> result = voltage
  .OrderBy(s =>
  {
    int i = 0;
    return int.TryParse(s, out i) ? i : int.MaxValue;
  })
  .ThenBy(s => s)
  .ToList();
like image 23
Amy B Avatar answered Sep 27 '22 23:09

Amy B