给你一个最简单的冒泡排序代码:
将三个数放到一个数组中.
dim flag as Boolean,temp as Integer
flag = true
if a(j)a(j-1) then
temp = a(j-1)
a(j-1) = a(j)
a(j) = temp
flag = false
end if
next j
if flag then Exit For
next i
你直接传一个数组进去,而且是一个结构体数组,array.sort怎么知道根据结构中的哪一个属性进行排序?放一个c#的代码你看看,VB和C#很相似的
class Program
{
static void Main(string[] args)
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 ;
如果你正努力学习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.
Console.WriteLine("After sorting a section of the Array using the default comparer:")
' Sorts a section of the Array using the reverse case-insensitive comparer.
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
' 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:")
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
'After sorting a section of the Array using the default comparer:
' [1] : BROWN
'After sorting a section of the Array using the reverse case-insensitive comparer:
'After sorting the entire Array using the default comparer:
' [0] : BROWN
' [1] : dog
'After sorting the entire Array using the reverse case-insensitive comparer:
' [0] : the
' [1] : The
以上就是土嘎嘎小编为大家整理的java前台代码举例相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!