[英]Linq - Order by number then letters
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
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! 谢谢您的帮助!
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: 鉴于你正在进行所有进程(因为你有一个ToList调用)我想我只是使用自定义比较器:
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: AlphaNumericComparer
实现IComparer<string>
,如下所示:
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 :) ...但在这种情况下我不认为我会:)
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.