你直接傳一個數(shù)組進(jìn)去,而且是一個結(jié)構(gòu)體數(shù)組,array.sort怎么知道根據(jù)結(jié)構(gòu)中的哪一個屬性進(jìn)行排序?放一個c#的代碼你看看,VB和C#很相似的

為企業(yè)提供網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站優(yōu)化、全網(wǎng)整合營銷推廣、競價托管、品牌運(yùn)營等營銷獲客服務(wù)。創(chuàng)新互聯(lián)擁有網(wǎng)絡(luò)營銷運(yùn)營團(tuán)隊,以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!
class Program
{
static void Main(string[] args)
{
People[] p = new People[3]
{
new People{name="張三"},
new People{name="李四"},
new People{name="張二名"}
};
//重點(diǎn)傳一個實現(xiàn)了IComparer接口的類進(jìn)去,告訴Array.Sort怎么排序
Array.Sort(p, new PeopleCompare());
foreach (var item in p)
{
Console.WriteLine(item.name);
}
Console.ReadKey();
}
}
//People結(jié)構(gòu)體,換成類一樣的
public struct People
{
public string name { get; set; }
}
//實現(xiàn)了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);
}
}
看了你說遞歸的效率低。那么你可以不用的。
給出的方法就是先生成第一個排列,然后每次調(diào)用下面的函數(shù)給出下一個排列,這樣生成的效率很高,這個函數(shù)可以內(nèi)聯(lián)。
這個是很經(jīng)典的排列組合算法啊?在網(wǎng)上能搜到一大堆。
大概是那種帶指向的移動的算法。我給你搜一個吧。
我找了幾個,這個是我覺得說的比較清楚的,你可以仔細(xì)參考一下,看不懂的話再搜點(diǎn)別的好了。。
全排列的算法跟這個不太一樣的。需要有點(diǎn)改動的。
至于語言的話,應(yīng)該不會有太大問題吧。。basic版的確實比較少,現(xiàn)在我也比較懶不想動手寫。。還是要靠你自己啦。
★生成排列的算法:
比如要生成5,4,3,2,1的全排列,首先找出一個最小的排列12345, 然后依次調(diào)用n!次STL算法中的next_permutation()即可輸出所有的全排列情況。所以這種算法的細(xì)節(jié)就是STL algorithm中next_permutation()的實現(xiàn)機(jī)制。詳細(xì)的實現(xiàn)代碼,大伙可以參考侯捷的《STL源代碼剖析》,在這里我只說一下我的理解:
1 首先從最尾端開始往前尋找兩個相鄰元素,令第一個元素為*i,第二個元素為*ii,且滿足*i*ii,找到這樣一組相鄰的元素后。
2 再從最尾端開始往前檢驗,找出第一個大于*i的元素,令為*k,將i,k元素對調(diào)。
3 再將ii及ii之后的所有元素顛倒排列,此即所求之"下一個"排列。
prev_permutation()算法的思路也基本相同,只不過它們尋找的"拐點(diǎn)"不同,在next_permutation()算法中尋找的是峰值拐點(diǎn),而在prev_permutation()算法中尋找的是谷值拐點(diǎn)。另外,在第二步中,prev_permutation()要找的是第一個小于*i的元素而不是第一個大于*i的元素。
具體例子,有空再舉,現(xiàn)在時間太晚了:)
★生成組合的算法:
如下面截圖所示,分全組合和r-組合兩種情況。
這里有一段核心代碼:
//--------------------------------------------------------
// Generate next combination (algorithm from Rosen p. 286)
//--------------------------------------------------------
public int[] getNext () {
if (numLeft.equals (total)) {
numLeft = numLeft.subtract (BigInteger.ONE);
return a;
}
int i = r - 1;
while (a[i] == n - r + i) {
i--;
}
a[i] = a[i] + 1;
for (int j = i + 1; j r; j++) {
a[j] = a[i] + j - i;
}
numLeft = numLeft.subtract (BigInteger.ONE);
return a; //這里返回的a數(shù)組,存儲的就是下標(biāo)的排列組合。
}
到這里,也許大伙會有一個疑問,假如要求的不是數(shù)字的排列組合,而是字符或字符串的排列組合呢?怎么辦?其實很簡單,你只要拿數(shù)組的下標(biāo)來做排列組合,返回他們下標(biāo)的排列組合,然后再到原數(shù)組中讀取字符串值,就可以輸出全部的排列組合結(jié)果。
如果你是從vb6剛過渡上vb。net,建議還是用冒泡排序法,容易理解。
如果你正努力學(xué)習(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.
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
好象有個StrReverse函數(shù)是直接反轉(zhuǎn)字符串的吧..
我平時用的是VB6.但是.NET中應(yīng)該也是可以用MID的
你注意看FOR 是從len-1到0的..而mid中是不允許出現(xiàn)0的..
你看看MID運(yùn)行不了時,是不是返回I是0的時間
改成:
For i = Len(sstr) To 1 Step -1
試試了?
加了單引號就是一個常量字符串了,對于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了
第一題:
不需要任何控件,代碼如下:
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("數(shù)字的個數(shù)", "輸入", Int(Rnd * 100))
If St = "" Then
MsgBox "請輸入數(shù)字!"
Else
N = Int(Val(St))
If N 1 Then
MsgBox "請輸入大于0的數(shù)字!"
Else
Exit Do
End If
End If
Loop
ReDim A(N)
For I = 1 To N
Do
St = InputBox("第" + Str(I) + "個數(shù)字", "輸入", Int(Rnd * 100))
If St = "" Then
MsgBox "請輸入數(shù)字!"
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 "已經(jīng)把"; N; "個數(shù)寫入到"; App.Path; "\1.txt 到 "; N; ".txt中.請查看."
End Sub
'已經(jīng)運(yùn)行過.
第二題:
DIM 是變量聲明語句,它的格式為:
dim 變量名[as 格式] [,變量名[as 格式][,變量名[as 格式]......]
其中:
變量名:以字母或漢字開始的字串,代表一個變量
格式有以下幾種:
屬于數(shù)字的有五種:
(1)字節(jié)型:byte可取值0-255
(2)整形:integer可取值-32768至32767
(3)長整形:long(可取值范圍很大的正負(fù)整數(shù))
(4)單精度型:single(可取值小數(shù))
(5)雙精度型:double(可取值范圍更大,小數(shù)位數(shù)更多的小數(shù))
字符串型:string(可代表由字母\數(shù)字或漢字組成的字符集合)
布爾型:boolean(取值為ture\false)
日期型:date(可表示形如2009-5-26 02:36這樣的組合)
如果要用姓名\住址\單位名稱...等用字符串型(string)
eg:dim name as string(用name變量表示名字時,聲明成字符串變量)
如果是用數(shù)字需要做計算,如工資\合計\人數(shù)....等要用數(shù)字型,但有一個原則,優(yōu)先選用范圍小的(按照字節(jié)型(byte)\整形(integer)\長整形(long)\單精度型(single)\雙精度型(double)的順序選擇),夠用就可以了,這樣可以占用內(nèi)存少,運(yùn)算速度快.
eg:dim count as integer(用integer表示員工人數(shù)時,可聲明成整形變量)
eg:dim sum as single(用sum表示工資時,可聲明成單精度型變量)
不知是否說得清楚了.
名稱欄目:vb.net字符排序,vb字母排序
URL網(wǎng)址:http://chinadenli.net/article32/hsjcsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、品牌網(wǎng)站建設(shè)、微信公眾號、企業(yè)建站、營銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)