在字符串中排列排序顺序

时间:2013-08-06 16:02:41

标签: .net arrays string linq

[可能重复] How to customize sort order of strings

关于订购此清单所述的同一问题:

bufallo@2000
lice@20
cell@1
rat@150
cow@10000

...进入此列表:

cow@10000
bufallo@2000
rat@150
lice@20
cell@1

假设我忽略了小于100的数字部分 - 也就是说,要删除其他两个:

cow@10000
bufallo@2000
rat@150

这可能吗?我已经尝试了下面的代码,但我不知道如何自定义小于100的数字部分,从而删除其余部分。

Dim number As Int32 = Int32.MinValue
Dim orderedLines = From line In TextBox1.Lines
                   Let parts = line.Split("@"c)
                   Let numericPart = parts.Last()
                   Let success = Int32.TryParse(numericPart, number)
                   Select LineInfo = New With {line, number}
                   Order By LineInfo.number Descending
                   Select LineInfo.line
' if you want to reassign it to the TextBox:
TextBox1.Lines = orderedLines.ToArray()

1 个答案:

答案 0 :(得分:1)

我的VB LINQ体验非常稀少,但这是你想要的吗?

Dim orderedLines = From line In TextBox1.Lines
                   Let parts = line.Split("@"c)
                   Let numericPart = parts.Last()
                   Let success = Int32.TryParse(numericPart, number)
                   Select LineInfo = New With {line, number}
                   Where number >= 100      ' something like this?
                   Order By LineInfo.number Descending
                   Select LineInfo.line