vb.net排序代码,vb排序算法

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);

}

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 排列组合 代码

第一题:

不需要任何控件,代码如下:

Private Sub Form_Click()

Dim A() As Integer, N As Integer

Dim St As String, I As Integer, J As Integer

Randomize

Do

St = InputBox("数字的个数", "输入", Int(Rnd * 100))

If St = "" Then

MsgBox "请输入数字!"

Else

N = Int(Val(St))

If N 1 Then

MsgBox "请输入大于0的数字!"

Else

Exit Do

End If

End If

Loop

ReDim A(N)

For I = 1 To N

Do

St = InputBox("第" + Str(I) + "个数字", "输入", Int(Rnd * 100))

If St = "" Then

MsgBox "请输入数字!"

Else

A(I) = Int(Val(St))

Exit Do

End If

Loop

Next

For I = 1 To N - 1

For J = I + 1 To N

If A(I) A(J) Then

A(0) = A(I)

A(I) = A(J)

A(J) = A(0)

End If

Next

Next

For I = 1 To N

Open App.Path "\" Trim(Str(I)) ".txt" For Output As #1

Print #1, A(I)

Close #1

Next

Print "已经把"; N; "个数写入到"; App.Path; "\1.txt 到 "; N; ".txt中.请查看."

End Sub

'已经运行过.

第二题:

DIM 是变量声明语句,它的格式为:

dim 变量名[as 格式] [,变量名[as 格式][,变量名[as 格式]......]

其中:

变量名:以字母或汉字开始的字串,代表一个变量

格式有以下几种:

属于数字的有五种:

(1)字节型:byte可取值0-255

(2)整形:integer可取值-32768至32767

(3)长整形:long(可取值范围很大的正负整数)

(4)单精度型:single(可取值小数)

(5)双精度型:double(可取值范围更大,小数位数更多的小数)

字符串型:string(可代表由字母\数字或汉字组成的字符集合)

布尔型:boolean(取值为ture\false)

日期型:date(可表示形如2009-5-26 02:36这样的组合)

如果要用姓名\住址\单位名称...等用字符串型(string)

eg:dim name as string(用name变量表示名字时,声明成字符串变量)

如果是用数字需要做计算,如工资\合计\人数....等要用数字型,但有一个原则,优先选用范围小的(按照字节型(byte)\整形(integer)\长整形(long)\单精度型(single)\双精度型(double)的顺序选择),够用就可以了,这样可以占用内存少,运算速度快.

eg:dim count as integer(用integer表示员工人数时,可声明成整形变量)

eg:dim sum as single(用sum表示工资时,可声明成单精度型变量)

不知是否说得清楚了.

VB.NET 2维数组排序排序和赋值问题

窗体上添加3个标签,1个按钮,在按钮的单击事件里写代码,如下:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim a(,) As Integer = {{5, 6}, {1, 3}, {8, 9}, {72, 1}, {63, 4}}

Dim Temp As Integer

Dim i As Integer

Dim j As Integer

Dim x As Integer

Dim y As Integer

'显示排序前的数据

Label1.Text = "排序前数据:" vbCrLf

For i = 0 To 4

Label1.Text = Label1.Text a(i, 0) " " a(i, 1) vbCrLf

Next

For i = 0 To 3

For j = i + 1 To 4

If a(i, 0) a(j, 0) Then

Temp = a(i, 0)

a(i, 0) = a(j, 0)

a(j, 0) = Temp

Temp = a(i, 1)

a(i, 1) = a(j, 1)

a(j, 1) = Temp

End If

Next

Next

'显示排序前的数据

Label2.Text = "排序后数据:" vbCrLf

For i = 0 To 4

Label2.Text = Label2.Text a(i, 0) " " a(i, 1) vbCrLf

Next

'把第3行元素赋予X,Y

x = a(2, 0)

y = a(2, 1)

'输出X,Y

Label3.Text = "X=" x vbCrLf "Y=" y

End Sub

vb.net的随机3个数字,然后排序一下

给你一个最简单的冒泡排序代码:

将三个数放到一个数组中。

a(0)=val(text1.text):a(1)=val(text2.text):a(2)=val(text3.text)

dim flag as Boolean,temp as Integer

for i = 0 to 2

flag = true

for j = 2 to 1

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

text4.text=a(0):text5.text=a(1):text6.text=a(2)

编写一个 VB.NET 程序,产生 100 个 100 以内的随机数,将他们从大到小排序后输出

Private Sub Command1_Click()

Dim a(1 To 100) As Integer

Dim i As Integer, j As Integer, k As Integer

For i = 1 To 100 '给数组a一百个元素赋值,并换每行十个数字输出来窗体上

a(i) = Int(Rnd * 101)

k = k + 1

Print Tab((k - 1) * 5); a(i);

If k = 10 Then k = 0: Print

Next i

Print

Print

For i = 100 To 2 Step -1 '用冒泡排序法对数组进行排序

For j = 1 To i - 1

If a(j) a(j + 1) Then

t = a(j): a(j) = a(j + 1): a(j + 1) = t

End If

Next j

Next i

For i = 1 To 100 '输出排好序的数组

k = k + 1

Print Tab((k - 1) * 5); a(i);

If k = 10 Then k = 0: Print

Next i

End Sub

vb.net冒泡排序法代码

试试看:

For i = LBound(moto) To UBound(moto) - 1

For j = LBound(moto) To UBound(moto) - 1 - i

If moto(j)  moto(j + 1) Then

t = moto(j)

moto(j) = moto(j + 1)

moto(j + 1) = t

End If

Next j

Next i

For i = LBound(moto) To UBound(moto)

Print moto(i);

Next i


本文题目:vb.net排序代码,vb排序算法
浏览路径:http://ybzwz.com/article/dsidgsd.html