扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
你直接传一个数组进去,而且是一个结构体数组,array.sort怎么知道根据结构中的哪一个属性进行排序?放一个c#的代码你看看,VB和C#很相似的
创新互联是一家专业提供黄州企业网站建设,专注与成都做网站、网站建设、外贸营销网站建设、H5场景定制、小程序制作等业务。10年已为黄州众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
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);
}
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);
}
}
Sub Main()
Dim s()
s() = Array(0, 1, 1, 0, 2, 2, 3, 3, 3, 4, 5, 8, 0, 1, 6, 8, 3, 7)
Dim mr(1 To 6, 1 To 3) As Byte
Dim i As Long, j As Long, n As Long, st As String
For i = 1 To 6
For j = 1 To 3
mr(i, j) = s(n): n = n + 1
st = st mr(i, j) ","
Next
st = st vbCrLf
Next
Debug.Print st
'从这里开始
Dim x As Long, y As Long
Dim t1, t2, t3
For x = 1 To 5
For y = x + 1 To 6
If (mr(y, 1) 0 And mr(x, 1) mr(y, 1)) Or mr(x, 1) = 0 Then
'将符合条件的元素交换
t1 = mr(x, 1): t2 = mr(x, 2): t3 = mr(x, 3)
mr(x, 1) = mr(y, 1): mr(x, 2) = mr(y, 2): mr(x, 3) = mr(y, 3)
mr(y, 1) = t1: mr(y, 2) = t2: mr(y, 3) = t3
End If
Next
Next
st = "================" vbCrLf
For i = 1 To 6
If mr(i, 1) = 0 Then Exit For
For j = 1 To 3
st = st mr(i, j) ","
Next
st = st vbCrLf
Next
Debug.Print st
End Sub
Private Sub Command4_Click()
Dim t As clerk, i%, j%
For i = 0 To n - 1
For j = i To n - 2
If a(i).vc a(j + 1).vc Then
t = a(i): a(i) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
Picture2.Cls
Picture2.Print "学号 姓名 VC VB"
Picture2.Print "---------------------------------------------"
For i = 0 To n - 1
Picture2.Print a(i).number, a(i).name, a(i).vc, a(i).vb
Next i
End Sub
扩展资料
vb数组排序思路:
1、冒泡排序法:
位置相邻两数进行两两比较,在比较时如果发现前面的数比后面的数大,则进行交换,都比较完一轮后,把最大一个数放到最后,如此进行下去即可完成冒泡排序。
2、比较交换法
假设第一个数最小,然后第一个数依次与后面的每个数都进行比较, 若比较时发现后面的数比第一个数小, 则两数位置进行交换, 全部都比较完算一轮,每一轮比较完后,第一个数是最小的数,如此进行即可完成比较排序。
3、选择排序
假设第一个数最小,接着记下最小数所在的位置,然后将最小数依次与后面的每一个数都进行比较,若比较时发现后面的数比最小的数还小,则修改最小数所在位置,全部都比较完算一轮。
每一轮比较完后,最小数所在的位置是否跟假设的是同一个位置,若不是,则最小数与第一个数进行交换位置,如此进行即可完成选择排序。
游戏中遇到这样的问题,需要将一组已知的数据打乱,按照以前和现在的做法,总结了以下方法。
方法一,最笨的菜鸟方法,也是容易想到的(幸好我没想过这种方法 :))
从已知数组中随机一个数,然后加入到另一个数组中,在加入之前,先检查是否已经加入过。
这种方法有很大运气成分,且数据越大,效率越低,超过一定数目,则程序几乎无法执行,会一直卡在那里,代码:
[java] view plain copy
package com.test;
import java.util.Random;
public class TestArray {
public static int runCount =0;//用于记录方法运算次数
Dim i As Integer, j As Integer, X As Single, Y As Single, M As Single
i = L
j = R
'找出数组的中点
M = MyArray((L + R) / 2, 0)
While (i = j)
'找出比中点大的数
While (MyArray(i, 0) M And i R)
i = i + 1
Wend
'找出比中点小的数
While (M MyArray(j, 0) And j L)
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 = i + 1
j = j - 1
End If
Wend
'未完成时递归调用
If (L j) Then Call QuickSort(MyArray(), L, j)
If (i R) Then Call QuickSort(MyArray(), i, R)
End Sub
如果你是从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
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流