Classement de 2 chaines par ordre lexicographique (alphabetique)

Description

Comme le titre le dit le programme classe deux chaines par ordre alphabetique.

Source / Exemple :


'Dans la source du Zip il y a des commentaires

Function Class(Str1 As String, Str2 As String) As Integer
Dim i As Integer                      
Dim Temp1 As String                   
Dim Temp2 As String                     
Dim Lenght As Integer                  
Dim State As Integer                  
                                       
Const EqualStrings = 1                    
Const String2IsFirst = 2                 
Const String1IsFirst = 3                  

On Error Resume Next                    

Str1 = Trim(Str1)                       
Str2 = Trim(Str2)                       

If Len(Str2) < Len(Str1) Then           
    Lenght = Len(Str2)                  
Else                                    
    Lenght = Len(Str1)
End If

For i = 1 To Lenght                    
    Temp1 = Mid(Str1, i, 1)             
    Temp2 = Mid(Str2, i, 1)             
    
    If Asc(Temp1) = Asc(Temp2) Then     
        State = EqualStrings            
            If Len(Str1) > Len(Str2) Then       
                State = String2IsFirst          
            ElseIf Len(strstr1) < Len(Str2) Then    
                State = String1IsFirst              
            End If                              
    ElseIf Asc(Temp1) > Asc(Temp2) Then 
        State = String2IsFirst          
        Exit For                        
    ElseIf Asc(Temp1) < Asc(Temp2) Then 
        State = String1IsFirst          
        Exit For                        
    End If                              
Next i                                  

Class = State                           

End Function

Private Sub Command1_Click()
Dim Result  As Integer
Const EqualStrings = 1         
Const String2IsFirst = 2
Const String1IsFirst = 3

Result = Class(Text1.Text, Text2.Text)    
Label2.Caption = ""                
Label3.Caption = ""                 

If Result = String1IsFirst Then         
    Label2.Caption = "1 - " & Text1.Text    
    Label3.Caption = "2 - " & Text2.Text    
ElseIf Result = String2IsFirst Then        
    Label2.Caption = "1 - " & Text2.Text
    Label3.Caption = "2 - " & Text1.Text
ElseIf Result = EqualStrings Then        
    Label1.Caption = "Les deux chaines sont identiques"
    Label2.Caption = ""
End If

End Sub

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.