LINQ的 - 按号码,然后字母顺序 [英] Linq - Order by number then letters

查看:136
本文介绍了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,室温,400 - > LINQ - > 1,
5分配,400,500,LT,RT

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!

推荐答案

既然你正在做的这一切过程(因为你已经有了一个了ToList电话)我想我会只需使用一个自定义比较:

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();



其中, AlphaNumericComparer 工具的IComparer<串> ,是这样的:

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 :)

这篇关于LINQ的 - 按号码,然后字母顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆