Manipulating a text field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My table contains a text field which consists of string of 13 integers. I
need to re-write tthe field so that the integers are in descending oder of
magnitude. So I need to replace a string such as "2460129883300" with
"9886433221000".

Any help would be much appreciated.

Jim Jones
Botswana
 
Put each digit into a separate element array, sort the array, then
concatenate back. Something along the lines of the following untested
air-code:

Function SortDigits(InputValue As String) As String

Dim intLoop1 As Integer
Dim intLoop2 As Integer
Dim strArray(13) As String
Dim strTemp As String

If Len(InputValue) <> 13 Then
SortDigits = "ERROR"
Else
For intLoop1 = 1 to 13
strArray(intLoop1) = _
Mid(InputValue, intLoop1, 1)
Next intLoop1
For intLoop1 = 13 To 1 Step -1
For intLoop2 = 2 To intLoop1
If strArray(intLoop2 - 1) < strArray(intLoop2) Then
strTemp = strArray(intLoop2 - 1)
strArray(intLoop2 - 1) = strArray(intLoop2)
strArray(intLoop2) = strTemp
End If
Next intLoop2
Next intLoop1
For intLoop1 = 1 to 13
SortDigits = SortDigits & strArray(intLoop1)
Next intLoop1
End If

End Function
 
I wish Microsoft would give us a high level language:

sub sortDigits { join '', sort {$b cmp $a} split '', $_[0] }
 
Many thanks for this. Since posting the question, I managed the sort using a
large number of update queries and the MID function applied manually one
after the other. It worked, but now I will try and do it properly in the way
which you suggest.

Thanks again

Jim Jones
Botswana
 
Thanks for the response

Jim Jones
Botswana

John Nurick said:
I wish Microsoft would give us a high level language:

sub sortDigits { join '', sort {$b cmp $a} split '', $_[0] }


Put each digit into a separate element array, sort the array, then
concatenate back. Something along the lines of the following untested
air-code:

Function SortDigits(InputValue As String) As String

Dim intLoop1 As Integer
Dim intLoop2 As Integer
Dim strArray(13) As String
Dim strTemp As String

If Len(InputValue) <> 13 Then
SortDigits = "ERROR"
Else
For intLoop1 = 1 to 13
strArray(intLoop1) = _
Mid(InputValue, intLoop1, 1)
Next intLoop1
For intLoop1 = 13 To 1 Step -1
For intLoop2 = 2 To intLoop1
If strArray(intLoop2 - 1) < strArray(intLoop2) Then
strTemp = strArray(intLoop2 - 1)
strArray(intLoop2 - 1) = strArray(intLoop2)
strArray(intLoop2) = strTemp
End If
Next intLoop2
Next intLoop1
For intLoop1 = 1 to 13
SortDigits = SortDigits & strArray(intLoop1)
Next intLoop1
End If

End Function
 
Back
Top