![]() |
|
#1
|
|||
|
|||
|
Hi folks,
I would like to sort strings on MS Word based on their lenght in characters. For example, consider the following unordered array: This string is short This string is kinda big This string is a really big one This string is medium I would like it to be ordered as follows, according to the number of characters (including spaces) in each string: This string is short This string is medium This string is kinda big This string is a really big one Can someone help me? |
|
#2
|
|||
|
|||
|
Code:
Sub Sorter()
Dim arrToSort() As String
Dim lngIndex As Long
arrToSort = Split("This string is short," _
& "xxxxxxxxxx," _
& "aaaaaaaaaa," _
& "This string is kinda big," _
& "This string is a really big one," _
& "This string is medium", ",")
Sort_Bubble arrToSort
For lngIndex = 0 To UBound(arrToSort)
Debug.Print arrToSort(lngIndex)
Next lngIndex
End Sub
Public Sub Sort_Bubble(arrSort As Variant)
Dim lngIndex As Long
Dim lngNext As Long
Dim strTemp As String
For lngIndex = LBound(arrSort) To UBound(arrSort) - 1
For lngNext = lngIndex + 1 To UBound(arrSort)
If Len(arrSort(lngIndex)) > Len(arrSort(lngNext)) Then
strTemp = arrSort(lngNext)
arrSort(lngNext) = arrSort(lngIndex)
arrSort(lngIndex) = strTemp
ElseIf Len(arrSort(lngIndex)) = Len(arrSort(lngNext)) Then
'If string lenght is equal sort alphabetically.
If arrSort(lngIndex) > arrSort(lngNext) Then
strTemp = arrSort(lngNext)
arrSort(lngNext) = arrSort(lngIndex)
arrSort(lngIndex) = strTemp
End If
End If
Next lngNext
Next lngIndex
End Sub
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| display number of characters in a word section | shootingluc | Word | 3 | 03-29-2012 03:25 PM |
Highlight the first X number of characters
|
14spar15 | Word | 1 | 11-13-2011 11:17 PM |
update style of all strings available between two specific strings
|
vikrantkale | Word | 1 | 03-28-2011 06:13 PM |
Adding Characters to Page Number
|
Daved2424 | Word | 6 | 01-22-2011 07:11 PM |
Sort by criteria: number of results
|
p0k | Excel | 1 | 10-22-2009 08:33 PM |