项目将以x,y和w开头,而我得到的排序顺序是:x首先以降序排列,然后y以降序排列,最后w以升序排列,只有以x,y和w开头的项目才会出现。 例如,
输入
y1
w1
y2
x1
x3
w10
w19
输出
x3
x1
y2
y1
w1
w10
w19
我尝试过的事情:
公共类表格1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Clear()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox2.Items.AddRange(ListBox1.Items)
ListBox2.Sorted = True
Dim StrArray As String() = ListBox2.Items.OfType(Of String).ToArray()
Dim str1() As String
Dim str2() As String
Dim str3() As String
For i As Integer = 0 To StrArray.Length - 1
If (StrArray(i).Substring(0, 1) = "x") Then
str1(i) = StrArray(i)
ElseIf (StrArray(i).Substring(0, 1) = "y") Then
str2(i) = StrArray(i)
Else
str3(i) = StrArray(i)
Array.Sort(str3)
End If
Next i
str1 = str1.Reverse
str2 = str2.Reverse
ListBox2.Items.AddRange(str1.ToArray)
ListBox2.Items.AddRange(str2.ToArray)
ListBox2.Items.AddRange(Array.Sort(str3))
End Sub
结束班级
答案 0 :(得分:0)
在控制台项目中尝试。是LINQ。
最后一次Select()方法将“头”和“值”组合起来。
模块Module1
Sub Main()
Dim arr = {"y1", "w1", "y2", "x1", "x3", "w10", "w19"}
Dim sortedArr = arr _
.Select(Function (x) New With { .Head = x(0), .Value = Convert.ToInt32(x.SubString(1, x.Length - 1)) }) _
.OrderBy(Function(x) If(x.Head = "x", 1, If(x.Head = "y", 2, 3))) _
.ThenBy(Function(x) If(x.Head = "x" Or x.Head = "y", -x.Value, x.Value)) _
.Select(Function(x) x.Head & x.Value)
For Each value In sortedArr
Console.WriteLine(value)
Next
Console.ReadKey()
End Sub
最终模块