2011-01-13 9 views
0

J9の後にJ10とJ11を配置するVBで配列を並べ替える方法はありますか?VBでの文字列の並べ替えの問題

J1 (PN= 605848)   
J10 (PN= 605987)   
J11 (PN= 605987)   
J2 (PN= 605848)   
J3 (PN= 605836)   
J4 (PN= 605848)   
J5 (PN= 605848)   
J6 (PN= 605848)   
J7 (PN= 605189)   
J7B (PN= 605189)   
J7E (PN= 605189)   
J7F (PN= 605189)   
J7I (PN= 605189)   
J7J (PN= 605189)   
J7M (PN= 605189)   
J7N (PN= 605189)   
J8 (PN= 605987)   
J9 (PN= 605987)  

これは私がmyArray.sortを(実行した後に私が得るものです)

ありがとうございました。

MicSimは正しかった、あなたがあなた自身のコンパレータを使用する必要があります:あなたはVB.Netのいくつかのバージョンを使用していると仮定すると、

+0

あなたは何VBバージョンについては言及しませんでした。私はそれがいくつかの.NETバージョンだと思います。次に、このカスタムソートを行う独自のコンパレータを書くことができます。 – MicSim

答えて

1

。それを行う方法は次のとおりです。

コンパレータは、inherits Comparer(Of何か)のいずれかのクラスです。あなたの場合、ソートする値は文字列であるため、Comparer(Of String)を継承する必要があります。実際には配列をソートする

' If this class is frequently used, it should be in it's own source file. 
' If it's used only in one place, put it at the end of that source file. 

' This class is used to compare strings of the form L123 
' where L is a letter A-Z 
' and 123 is an integer 
' The goal here is to make sure that "L9" comes before "L10", 
' which isn't what we would get with simple string comparisons. 
Class CompLetterInteger 
    Inherits Comparer(Of String) 
    Public Overrides Function Compare(ByVal x As String, ByVal y As String) As Integer 
     ' Can value "Nothing" be in the array? 
     ' This makes sure that they come at the beginning of the array. 
     ' Skip these 8 lines if value Nothing is impossible. 
     If x Is Nothing Then 
      If y Is Nothing Then 
       Return 0 ' Values sort the same 
      End If 
      Return -1 ' X comes before Y 
     ElseIf y Is Nothing Then 
      Return 1 ' Y comes before X 
     End If 

     ' Here we parse both arguments into the first letter, 
     ' and then the numeric part. You might have to adjust 
     ' this for your data - if value "J123X" is possible, 
     ' you're going to have to adjust this. 
     Dim x1 As String = x.Substring(0, 1) 
     Dim x2 As Integer = 0 
     Integer.TryParse(x.Substring(1), x2) 

     Dim y1 As String = y.Substring(0, 1) 
     Dim y2 As Integer = 0 
     Integer.TryParse(y.Substring(1), y2) 

     ' Now decide which value should come first. 
     ' -1 means that X should come first, 
     ' +1 means that Y should come first, 
     ' 0 means that they sort the same. 
     If x1 < y1 Then Return -1 ' The letter of X is before the letter of Y 
     If x1 > y1 Then Return 1 ' The letter of X is after the letter of Y 
     ' The letters are equal, so look at the numeric part 
     If x2 < y2 Then Return -1 ' The number of X is less than the number of Y 
     If x2 > y2 Then Return 1 ' The number of X is more than the number of Y 
     Return 0 ' The two strings sort the same 
     ' Note that this does not mean that the two strings are identical. 
     ' "Y99" would sort the same as "Y099", 
     ' because the letters are the same and the numbers are the same value. 
    End Function 
End Class 

Dim Arr() As String = {"J1", "J9", "J10", "J11"} ' Etc. 
Array.Sort(Arr, New CompLetterInteger) 
関連する問題