Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-04-2014, 05:25 AM
reneforster reneforster is offline Sort strings by number of characters Windows 7 32bit Sort strings by number of characters Office 2007
Novice
Sort strings by number of characters
 
Join Date: Dec 2014
Posts: 1
reneforster is on a distinguished road
Default Sort strings by number of characters

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?
Reply With Quote
  #2  
Old 12-04-2014, 07:25 AM
gmaxey gmaxey is offline Sort strings by number of characters Windows 7 32bit Sort strings by number of characters Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



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
Sort strings by number of characters Highlight the first X number of characters 14spar15 Word 1 11-13-2011 11:17 PM
Sort strings by number of characters update style of all strings available between two specific strings vikrantkale Word 1 03-28-2011 06:13 PM
Sort strings by number of characters Adding Characters to Page Number Daved2424 Word 6 01-22-2011 07:11 PM
Sort strings by number of characters Sort by criteria: number of results p0k Excel 1 10-22-2009 08:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft