Results 1 to 4 of 4

Thread: Sorting

  1. #1
    Guest
    hi all,
    I want to know how to do sorting by pos in the alphabet.
    suppose i use the word BASIC, i want to display it as
    ABCIS.

    is there any way 2 do this?

  2. #2
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Worldwide in the Sun
    Posts
    566
    Take a look at this

    Create a small form with a textbox and a cmdbutton

    ‘ General
    Dim MyArray(5)


    Sub rSort(lower As Long, upper As Long)
    Dim pivot As String
    Dim first As Long, last As Long, middle As Long
    ' Locate pivot
    first = lower ' lower pointer
    last = upper ' upper pointer
    middle = (first + last) / 2
    pivot = MyArray(middle)
    Do ' move pointers against each other
    While MyArray(first) < pivot
    first = first + 1
    Wend
    While MyArray(last) > pivot
    last = last - 1
    Wend
    If first <= last Then
    temp = MyArray(first)
    MyArray(first) = MyArray(last)
    MyArray(last) = temp
    first = first + 1
    last = last - 1
    End If
    Loop Until first > last
    If lower < last Then
    Call rSort(lower, last)
    End If
    If first < upper Then
    Call rSort(first, upper)
    End If
    End Sub

    Private Sub Command1_Click()
    'fill MyArray
    MyArray(0) = "B"
    MyArray(1) = "A"
    MyArray(2) = "S"
    MyArray(3) = "I"
    MyArray(4) = "C"

    ‘ call sort module
    'parameters are start (0), stop (4), because MyArray = 5
    Call rSort(0, 4)

    ‘ print result in txtbox
    Text1.Text = MyArray(0) + MyArray(1) + MyArray(2) + MyArray(3) + MyArray(4)
    End Sub

    Cheers
    Ray
    Ray

  3. #3
    Guest
    Can this be used for words of variable length

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Yes, because you are passing an array to the function and you are telling the function how big the array is.

    Here is another (very similar) way to do it using a recursive function called QuickSort.

    This will ask the user to input a string, then it will sort the string and display it in a message box. Please note that it uses the ascii values of the letters to sort, so all uppercase letters will come before lower case letters. You can modify this if you want to.

    Put a command button (Command1) on the form and paste the following code into the form's code window:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim myString As String
        Dim myArray() As String
        Dim Counter As Integer
        
        ' Get a word or phrase from the user
        myString = InputBox("Enter a word to sort")
        
        ' If they entered something, sort it and display it
        If Len(myString) > 0 Then
            ' First we have to deconstruct the string one character at a time
            ' and add each character to an array so we can pass the array
            ' to the function
            ReDim myArray(1 To Len(myString))
            For Counter = LBound(myArray) To UBound(myArray)
                myArray(Counter) = Mid$(myString, Counter, 1)
            Next ' Counter
            
            ' Now we sort the array
            QuickSort myArray, LBound(myArray), UBound(myArray)
            
            ' Clear our string so we can add the sorted array to it
            myString = ""
            
            ' Add the sorted array back into the string one character at at time
            For Counter = LBound(myArray) To UBound(myArray)
                myString = myString & myArray(Counter)
            Next ' Counter
            
            ' Display the sorted string
            MsgBox myString
        End If
    End Sub
    
    Private Sub QuickSort(SortList As Variant, ByVal First As Integer, _
        ByVal Last As Integer)
    '
    'Purpose:   This function sorts a list from lowest to highest (0 - 9 for
    '           numeric arrays, A - z for string arrays). This is a recursive
    '           function, meaning that it calls itself in order to solve
    '           the problem.
    '
    'Arguments: SortList: An array of items to be sorted (Variant)
    '           First:    The index of the first item in the array (Integer)
    '           Last:     The index of the last item in the array (Integer)
    '
    'Note:      There is no "return value". However, the argument list is
    '           returned with it's elements sorted. If you want to retain
    '           your original list, pass this function a copy of the list.
    '
        Dim Low As Integer, _
            High As Integer, _
            Temp As Variant, _
            TestElement As Variant
            
        Low = First
        High = Last
        TestElement = SortList((First + Last) / 2)
        
        Do
            Do While SortList(Low) < TestElement
                Low = Low + 1
            Loop
            
            Do While SortList(High) > TestElement
                High = High - 1
            Loop
            
            If (Low <= High) Then
                Temp = SortList(Low)
                SortList(Low) = SortList(High)
                SortList(High) = Temp
                Low = Low + 1
                High = High - 1
            End If
        Loop While (Low <= High)
        
        If (First < High) Then
            Call QuickSort(SortList, First, High)
        End If
        
        If (Low < Last) Then
            Call QuickSort(SortList, Low, Last)
        End If
    End Sub
    Hope that gets you started...
    ~seaweed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width