我必须对运行时输入的项目进行排序,但方式不同 [英] I have to sort items entered during run time but in different pattern

查看:76
本文介绍了我必须对运行时输入的项目进行排序,但方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目将以x,y和w开头,我应该按顺序排序,项目首先按降序排列x,然后按降序排列y,最后w按升序排列,只有以x,y和w开头的项目将是那里。

例如,



如果我在下面给出输入



y1

w1

y2

x1

x3

w10

w19





i应低于产量:



x3

x1

y2

y1

w1

w10

w19



我尝试过:



公开Class Form1



Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理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



Dim StrArray As String()= ListBox1.Items.OfType(Of String).ToArray()

Array.Sort(StrArray)

'ListBox2.Items.AddRange(ListBox1.Items)

'ListBox2.Sorted = True



'Dim StrArray As String()= ListBox2.Items.OfType(Of String).ToArray()





Dim str1()= New String( ){}

Dim str2()= New String(){}

Dim str3()= New String(){}

' Dim str2()As String

'Dim str3()As String





for i As Integer = 0到StrArray.Length - 1



If(StrArray(i).Substring(0,1)=x)然后

str1(i)= StrArray(i)



ElseIf(StrArray(i).Substring(0,1)=y)然后

str2(i)= StrArray(i)



Else

str3(i)= StrArray(i)





结束如果





下一页我









str1 = str1.Reverse

str2 = str2.Reverse

Array.Sort( str3)



ListBox2.Items.AddRange(str1.ToArray)

ListBox2.Items.AddRange(str2.ToArray)

ListBox2.Items.AddRange(str3.ToArray)





End Sub





结束类

Items will start with x, y and w and i should get it sorted order with items starts with x first in descending order then y in descending order and last w in ascending order only items starting with x,y and w will be there.
for eg,

if i gave input below

y1
w1
y2
x1
x3
w10
w19


i should get below output:

x3
x1
y2
y1
w1
w10
w19

What I have tried:

Public Class Form1

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

Dim StrArray As String() = ListBox1.Items.OfType(Of String).ToArray()
Array.Sort(StrArray)
'ListBox2.Items.AddRange(ListBox1.Items)
'ListBox2.Sorted = True

'Dim StrArray As String() = ListBox2.Items.OfType(Of String).ToArray()


Dim str1() = New String() {}
Dim str2() = New String() {}
Dim str3() = New 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)


End If


Next i




str1 = str1.Reverse
str2 = str2.Reverse
Array.Sort(str3)

ListBox2.Items.AddRange(str1.ToArray)
ListBox2.Items.AddRange(str2.ToArray)
ListBox2.Items.AddRange(str3.ToArray)


End Sub


End Class

推荐答案

问题是你正在设置

The problem is that you are setting
ListBox2.Sorted = True

这意味着ListBox本身将根据为其定义的排序顺序自动订购商品该项 - 在这种情况下,字符串是您添加到集合中的所有内容,因此排序顺序是字符串排序。将它们分成不同的集合并单独添加它们并不重要,Sorted属性意味着ListBox将自动重新排序它们。并且排序顺序将始终相同:字符串从左到右进行比较,第一个不同的字符控制整个排序顺序。

如果你想使用Sorted,那么你将不得不将每个字符串括在一个支持IComparable的类中,并将自己的排序算法添加到该类中(以及重写ToString以显示任何有用的内容):如何在Visual Basic .NET或Visual Basic 2005中使用IComparable和IComparer接口 [ ^ ]

which means that the ListBox itself will automatically order items according to the sort order defined for that item - in this case, a string is all you ever add to the collection, so the sort order is string sort. It doesn't matter that you break them into different collections and add them separately, the Sorted property means that the ListBox will automatically reorder them regardless. And that sort order will always be the same: the strings are compared from left to right, and the first different character controls the whole sort order.
If you want to use Sorted, then you will have to enclose each string in a class that supports IComparable and add your own sort algorithm to that class (as well as overriding ToString in order to display anything useful): How to use the IComparable and the IComparer interfaces in Visual Basic .NET or in Visual Basic 2005[^]


这篇关于我必须对运行时输入的项目进行排序,但方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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