Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我必须在运行时对输入的项目进行排序,但模式不同_Vb.net - Fatal编程技术网

Vb.net 我必须在运行时对输入的项目进行排序,但模式不同

Vb.net 我必须在运行时对输入的项目进行排序,但模式不同,vb.net,Vb.net,项目将以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.Cl

项目将以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

在控制台项目中尝试使用End类。我是林克

  • 第一个Select()方法将每行拆分为一个具有Head和Value的类型,例如Head=x,Value=3
  • 第二个OrderBy()方法根据Head(第一个字符)的值对数组进行升序排序。X得1分,Y得2分,W得3分。所以它按x->y->w的顺序排序
  • 第三个ThenBy()方法是另一种基于value的值(数字)的升序排序。X和Y为负值,w为正值
  • 最后一个Select()方法将Head和Value组合起来

    模块1

    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
    
    端模块


  • 您需要创建一个实现
    IComparer(Of String)
    接口的类,或者创建一个签名与
    Comparison(Of String)
    委托匹配的方法。然后,您可以调用一个
    Sort
    方法,该方法使用其中一个进行比较。这给了您一些需要研究的东西,所以您应该这样做,如果您在实现方面有问题,请返回。