Sorting Text

Somtimes, I want to sort a block of text. So just select it and let the bubble sort


Sub SortSelection ()
'DESCRIPTION: Sorts Selection
    Dim win
    set win = ActiveWindow  
    If win.type <> "Text" Then
        MsgBox "This macro can only be run when a text editor window is active."
        Exit Sub
    End If

    StartLine = ActiveDocument.Selection.TopLine
    EndLine = ActiveDocument.Selection.BottomLine       
    If EndLine < StartLine Then
        Temp = StartLine            
        StartLine = EndLine         
        EndLine = Temp      
    End If

    EndLine = EndLine - 1         

    If StartLine > EndLine Then
        Exit Sub
    End If

    bStop = false

    Do While Not bStop
        bStop = true
        For i = StartLine  To EndLine            
            ActiveDocument.Selection.GoToLine i         
            ActiveDocument.Selection.SelectLine
            FirstLine = ActiveDocument.Selection         

            ActiveDocument.Selection.GoToLine i + 1        
            ActiveDocument.Selection.SelectLine
            SecondLine = ActiveDocument.Selection         

            If FirstLine > SecondLine Then 
                bStop = false
                ActiveDocument.Selection.GoToLine i
	            ActiveDocument.Selection.StartOfLine
	            ActiveDocument.Selection.MoveTo i + 1, dsEndOfLine, dsExtend
                ActiveDocument.Selection = SecondLine +  FirstLine    
	            ActiveDocument.Selection.EndOfLine
	            ActiveDocument.Selection.Delete
            End If
        Next
    Loop
    ActiveDocument.Selection.GoToLine StartLine
    ActiveDocument.Selection.StartOfLine
    ActiveDocument.Selection.MoveTo EndLine + 1, dsEndOfLine, dsExtend
End Sub


Date Last Updated: February 12, 1999



Comments

  • This macro contains a nasty bug

    Posted by Legacy on 04/29/1999 12:00am

    Originally posted by: Chad Loder

    This macro contains a nasty bug. I believe that if
    the document is read only, the bubble sort may never
    exit (when the lines aren't already in order). This
    is because the lines don't get swapped (read only!),
    hence they stay out of order and the loop never exits.

    I have a better sort macro that first stores all the
    lines in the selection in a vector, then sorts them
    in memory using quicksort, then writes them all at
    once. This avoids the read only problem and is much
    faster because the sorting happens in memory, not in
    the live document.

    Reply
  • Works, leaves blank window but just press END

    Posted by Legacy on 02/22/1999 12:00am

    Originally posted by: Lee Patterson

    After the macro has run, your window will probably be blank. 
    
    Just press END and the text will all reappear.

    Reply
  • macro left the source file blank

    Posted by Legacy on 02/13/1999 12:00am

    Originally posted by: ljp

    Curious effect. Left my test source page totally blank.....

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds