VB.net 数组怎么按任意元素的顺序排序输出你直接传一个数组进去 , 而且是一个结构体数组,array.sort怎么知道根据结构中的哪一个属性进行排序?放一个c#的代码你看看 , VB和C#很相似的
class Program
{
static void Main(string[] args)
{
People[] p = new People[3]
{
new People{name="张三"},
new People{name="李四"},
new People{name="张二名"}
};
//重点传一个实现了IComparer接口的类进去,告诉Array.Sort怎么排序
Array.Sort(p, new PeopleCompare());
foreach (var item in p)
{
Console.WriteLine(item.name);
【vb.net数组乱序 vbnet数组排序方法】}
Console.ReadKey();
}
}
//People结构体,换成类一样的
public struct People
{
public string name { get; set; }
}
//实现了IComparer接口的类
public class PeopleCompare : IComparer
{
public int Compare(object x, object y)
{
People p1 = (People)x ;
People p2 = (People)y;
return p1.name.CompareTo(p2.name);
}
}
在VB.net中,如何把listbox中的数值进行排序Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Apple")
ListBox1.Items.Add("Cat")
ListBox1.Items.Add("Yellow")
ListBox1.Items.Add("Guilty")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Sorted = True
End Sub
End Class
VB.NET数组的排序法?如果你是从vb6刚过渡上vb 。net,建议还是用冒泡排序法 , 容易理解 。
如果你正努力学习vb 。net的方法,推荐一个例子如下:
Imports System
Imports System.Collections
Public Class SamplesArray
Public Class myReverserClass
Implements IComparer
' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare
End Class 'myReverserClass
Public Shared Sub Main()
' Creates and initializes a new Array and a new custom comparer.
Dim myArr As [String]() ={"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}
Dim myComparer = New myReverserClass()
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the default comparer.
Array.Sort(myArr, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myArr, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myArr, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myArr() As [String])
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine("[{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArray
'This code produces the following output.
'
'The Array initially contains the following values:
'[0] : The
'[1] : QUICK
'[2] : BROWN
'[3] : FOX
'[4] : jumps
'[5] : over
'[6] : the
'[7] : lazy
'[8] : dog
'
'After sorting a section of the Array using the default comparer:
'[0] : The
'[1] : BROWN
'[2] : FOX
'[3] : QUICK
'[4] : jumps
'[5] : over
'[6] : the
'[7] : lazy
'[8] : dog
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
'[0] : The
'[1] : QUICK
'[2] : FOX
'[3] : BROWN
'[4] : jumps
'[5] : over
'[6] : the
'[7] : lazy
'[8] : dog
'
'After sorting the entire Array using the default comparer:
'[0] : BROWN
'[1] : dog
'[2] : FOX
'[3] : jumps
'[4] : lazy
'[5] : over
'[6] : QUICK
'[7] : the
'[8] : The
'
'After sorting the entire Array using the reverse case-insensitive comparer:
'[0] : the
'[1] : The
'[2] : QUICK
'[3] : over
'[4] : lazy
'[5] : jumps
'[6] : FOX
'[7] : dog
'[8] : BROWN
VB.NET 二维数组问题根据你的语句 , 上下标是确定的,所以定义语句修改为:
Dim ary(UBound(temp), 1) As String '声明二维数组
vb.net 对象数组你只是定义了一个对象,类而已
首先要给对象设置变量 , 这个还不是数组
Public class As Single的class 应该是关键字请换一个名字
Dim KidsX(1 to 100) as kids
KidsX(1).classx=1
KidsX(1).grade=1
KidsX(1).name=”张某"
KidsX(2).classx=1
KidsX(2).grade=2
KidsX(2).name=”王某"
……
二维数组排序 vb.netDim i As Integer, j As Integer, X As Single, Y As Single, M As Single
i = L
j = R
'找出数组vb.net数组乱序的中点
M = MyArray((LR) / 2, 0)
While (i = j)
'找出比中点大vb.net数组乱序的数
While (MyArray(i, 0)M And iR)
i = i1
Wend
'找出比中点小vb.net数组乱序的数
While (MMyArray(j, 0) And jL)
j = j - 1
Wend
'互换这两个数
If (i = j) Then
X = MyArray(i, 0)
Y = MyArray(i, 1)
MyArray(i, 0) = MyArray(j, 0)
MyArray(i, 1) = MyArray(j, 1)
MyArray(j, 0) = X
MyArray(j, 1) = Y
i = i1
j = j - 1
End If
Wend
'未完成时递归调用
If (Lj) Then Call QuickSort(MyArray(), L, j)
If (iR) Then Call QuickSort(MyArray(), i, R)
End Sub
vb.net数组乱序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于vbnet数组排序方法、vb.net数组乱序的信息别忘了在本站进行查找喔 。
推荐阅读
- 拼多多如何推广提高销量,拼多多怎么做推广更有效
- 上海奇骏车友会公众号关注,上海奇骏车友会微信群
- 虚拟空间是干啥的,虚拟空间概念
- 山寨用什么cpu,山寨机配置都是真的吗
- go语言和算法哪个比较难 go语言和算法哪个比较难一点
- 新装3t硬盘怎么分区,3t硬盘怎么装win7
- redis内存相关方案,redis内存机制
- js函数零基础,js函数详解
- c语言time函数越 c语言time_t函数