I have a string of names I want to sort by last name. But don't really know where last name starts in the string
So I wrote a routine to locate the last name but ran into trouble with names with Suffixes so I added a section for it
Is there a better way to take a name (first name, middle initial, last name suffix) and flip it for sorting?
So I wrote a routine to locate the last name but ran into trouble with names with Suffixes so I added a section for it
Is there a better way to take a name (first name, middle initial, last name suffix) and flip it for sorting?
B4X:
Public Sub SortLastName(xName As String) As String
Dim Suffixes() As String = Array As String(" jr", "jr.", " sr", " sr.", " ii", " iii", " iv", " v", " 2nd", " 3rd", " 4th")
Dim TempName As String = xName.Trim.ToLowerCase
Dim Name As String = xName.Trim
Dim NameSuffix As String
Dim IndexLastName As Int
'----------------------------------------------------------------------------------------------
' Look for and remove any name Suffix we can find and save it for later
'----------------------------------------------------------------------------------------------
For Each Suffix As String In Suffixes
If TempName.EndsWith(Suffix) Then
'--------------------------------------------------------------------------------------
' Name ends with the current suffix - save it for later
'--------------------------------------------------------------------------------------
NameSuffix = Name.SubString(Name.Length-Suffix.Length)
'--------------------------------------------------------------------------------------
' Remove the suffix will add on later
'--------------------------------------------------------------------------------------
Name = Name.Replace(NameSuffix, "")
Exit
End If
Next
'----------------------------------------------------------------------------------------------
' Found first occurance of space from end of name
'----------------------------------------------------------------------------------------------
IndexLastName = Name.LastIndexOf(" ")
If IndexLastName = -1 Then
'------------------------------------------------------------------------------------------
' If we could not find where last names starts then just returned passed name
'------------------------------------------------------------------------------------------
Return xName
End If
'----------------------------------------------------------------------------------------------
' Pull what we think is Last Name and First Name
'----------------------------------------------------------------------------------------------
Dim LastName As String = $"${Name.SubString(IndexLastName + 1)}${NameSuffix}"$
Dim FirstName As String = Name.SubString2(0, IndexLastName)
'----------------------------------------------------------------------------------------------
' Pad the lastname to 40 chars so when we add on firstname should make the sort right
'----------------------------------------------------------------------------------------------
#if Debug
Log($"Sort Name:${PadString(LastName, 40)}, ${FirstName}"$)
#end if
Return $"${PadString(LastName, 40)}, ${FirstName}"$
End Sub
Public Sub PadString(StringToPad As String, PadTo As Int) As String
Dim Padder As Int
Dim NewString As String = StringToPad
If NewString.Length >= PadTo Then
If NewString.Length > PadTo Then
NewString = NewString.SubString2(0, PadTo-3) &"..."
End If
Else
For Padder = NewString.Length To PadTo-1
NewString = NewString &" "
Next
End If
Return NewString
End Sub
[/code}
Last edited: