Jump to content

Alphabetize Values In Arrays


Recommended Posts

I worked 2 hours on this, but I can't make it fully-functional.

It compares one value with the next. So it will not alphabetize the entire array.

I have no idea how to do it for the enitre array.

Can someone help me with this?

$TestArray = "Tweaks|Dictionary|Programming|Favorites|Downloads|Filesharing"

$Words = Alphabetize ($TestArray)

MsgBox(0, "Test", "$Words:" & @CRLF & $Words)

Func Alphabetize ($Words)
  $oldletter = ""
  $newletter = ""
  $Word = StringSplit($Words, "|")
  $Number = UBound($Word)
  $Number = $Number - 1
  
  $Words = ""
  For $c = 1 To $Number
    
    $num = $c + 1
    If $num > $Number Then ExitLoop
    
    $Word1 = $Word[$c]
    $Word2 = $Word[$num]
    
    $oldletter = StringLeft($Word1, 1)
    $oldletter = Asc(StringLower($oldletter))
    
    $newletter = StringLeft($Word2, 1)
    $newletter = Asc(StringLower($newletter))
    
    If $oldletter < $newletter Then
      If $Words = "" Then
        $Words = $Word1
      Else
        $CheckWords = StringSplit($Words, "|")
        $Check1 = StringInStr($CheckWords[$CheckWords[0]], $Word1)
        $Check2 = StringInStr($CheckWords[$CheckWords[0]], $Word2)
        If $Check1 = 0 Then
          $Words = $Words & "|" & $Word1
        EndIf
        If $Check2 = 0 Then
          $Words = $Words & "|" & $Word2
        EndIf
        
      EndIf
    Else
      If $Words = "" Then
        $Words = $Word2
      Else
        $CheckWords = StringSplit($Words, "|")
        $Check1 = StringInStr($CheckWords[$CheckWords[0]], $Word1)
        $Check2 = StringInStr($CheckWords[$CheckWords[0]], $Word2)
        If $Check2 = 0 Then
          $Words = $Words & "|" & $Word2
        EndIf
        If $Check1 = 0 Then
          $Words = $Words & "|" & $Word1
        EndIf
      EndIf
    EndIf
    Sleep(100)
  Next
  Return $Words
EndFunc  ;==>Alphabetize

Fixed some errors

Edited by SlimShady
Link to comment
Share on other sites

  • Developers

I worked 2 hours on this, but I can't make it fully-functional.

It compares one value with the next. So it will not alphabetize the entire array.

I have no idea how to do it for the enitre array.

Can someone help me with this?

do you want to sort the array ?

here's a udf that can SHELL Sort multi dimentional arrays..

;=====================================================
; sort an array with multiple dimentions
;=====================================================
Func sort_array(ByRef $I_ARRAY, $I_DIM, $I_UBOUND)
  ;shell sort array 
   $A_SIZE = $I_UBOUND
   $GAP = Int($A_SIZE / 2)
  ;
   While $GAP <> 0
      $ISCHANGED = 0
      For $COUNT = 1 To ($A_SIZE - $GAP)
         If $I_DIM = 1 Then
            If $I_ARRAY[$COUNT] > $I_ARRAY[$COUNT + $GAP] Then
               $TEMP = $I_ARRAY[$COUNT]
               $I_ARRAY[$COUNT] = $I_ARRAY[$COUNT + $GAP]
               $I_ARRAY[$COUNT + $GAP] = $TEMP
               $ISCHANGED = 1
            EndIf
         Else
            If $I_ARRAY[$COUNT][0] > $I_ARRAY[$COUNT + $GAP][0] Then
               For $C_DIM = 0 To $I_DIM - 1
                  $TEMP = $I_ARRAY[$COUNT][$C_DIM]
                  $I_ARRAY[$COUNT][$C_DIM] = $I_ARRAY[$COUNT + $GAP][$C_DIM]
                  $I_ARRAY[$COUNT + $GAP][$C_DIM] = $TEMP
                  $ISCHANGED = 1
               Next
            EndIf
         EndIf
      Next
     ;if no changes were made to array, decrease $gap size
      If $ISCHANGED = 0 Then
         $GAP = Int($GAP / 2)
      EndIf
   Wend
EndFunc  ;==>sort_array

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

There are different methodes of sorting and Shell is one of them.

There are a couple of functions that are posted by Nutster which will be included also in the next release of the UDF's...

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

A simple sorting routine is called Bubble Sort, because the small values seem to bubble to the top of the array. It works by going through the entire array looking for adjecent elements that are out of sequence, and swap them. It will then go through the array again, until no swaps occur, i.e. the array is sorted. It one of the slowest sorts, but it pretty simple to understand.

The Shell sort is a lot faster, but it is a lot more complicated. The Heap sort is a lot faster, but it is very difficult to follow what is happening. For a short list, Bubble sort works fine.

$testarray = "Tweaks|Dictionary|Programming|Favorites|Downloads|Filesharing"
$myarray = StringSplit($testarray, "|"); puts the number of elements as the first element, so it gets skipped for the rest.

DumpArray($myarray, "Before Sort")

BubbleSort($myarray)

DumpArray($myarray, "After Sort")

Func Swap(ByRef $v1, ByRef $v2)
   Local $tmp = $v1
   $v1 = $v2
   $v2 = $tmp
EndFunc;==>Swap

Func DumpArray(ByRef $array, $title)
   Local $i, $msg
   
   If Not IsArray($array) Then
       SetError(1)
   Else
      $msg = ""
      For $i = 1 To UBound($myarray) - 1
         $msg = $msg & ", " & $myarray[$i]
      Next
      $msg = StringStripWS(StringTrimLeft($msg, 2), 3)
      MsgBox(0, $title, $msg)
   Endif
EndFunc;==>DumpArray

Func BubbleSort(ByRef $array)
   Local $i, $swapped
      
   If Not IsArray($array) Then  ;Make sure that it is an array
      SetError(1)
      Return 
   ElseIf UBound($array, 0) <> 1 Then  ;Make sure that it is single dimensioned
      SetError(2)
      Return 
   Else; Here are the guts of it
      Do 
         $swapped = 0
         For $i = 0 To UBound($array) - 2
            If $array[$i] > $array[$i+1] Then
               Swap( $array[$i], $array[$i+1] )
               $swapped = 1
            EndIf
         Next
      Until $swapped = 0
   EndIf
EndFunc;==>BubbleSort
Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

I'm sorry. I'm not English and I try to understand you, but it's really difficult.

Can you explain again what it sorts and how, in easy-English.

You're using words I never saw or used. And in the script I can make out that it uses numbers + variables to sort... But how does it sort the values (eg.: Tweaks, Dictionary, Programming)

What I mean: How can I use this to sort variables in an array alphabetically?

Link to comment
Share on other sites

  • 2 weeks later...

Which words confused you? What is your native language?

There is the bubble sort routine with comments before each line.

Func BubbleSort(ByRef $array)
 ;  Create two variables.
 ; The first ($i) will keep track of which elements of the array we are checking.
 ; The second ($swapped) gets set if the routine swaps a pair of values.
  Local $i, $swapped
   
 ; Error checking:  Make sure that $array is actually an array.
  If Not IsArray($array) Then  ;Make sure that it is an array
    ;No?  Set an error condition and get out.
     SetError(1)
     Return
 ; UBound($x, 0) returns how many dimensions $x has.
 ; Check that the array only has one dimension.
  ElseIf UBound($array, 0) <> 1 Then  ;Make sure that it is single-dimensioned
    ;No?  Set an error condition and get out.
     SetError(2)
     Return
 ; It passes all the tests.  Here we go.
  Else; Here are the guts of it
     Do
       ; Set $swapped to indicate no swaps yet.
        $swapped = 0
       ; A loop to go from the 1st element of the array to the second last
        For $i = 0 To UBound($array) - 2
          ; Check if this element is greater than the next element
           If $array[$i] > $array[$i+1] Then
             ; If so, the elements are out of sequence, so swap them.  The Swap function does this.
              Swap($array[$i ], $array[$i+1] )
             ; Indicate that a swap has occured.
              $swapped = 1
           EndIf
        Next
    ; Keep going until there have been no swaps in a pass.
    ; If there has been a swap, then $swapped will be 1.
    ; The loop will continue until $swapped does not get set to 1.
     Until $swapped = 0
  EndIf
EndFunc;==>BubbleSort

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

What I didn't understand from your previous post was:

It works by going through the entire array looking for adjecent elements that are out of sequence, and swap them.

The part in bold got me confused.

Those comments make the script more clear, but not all.

Can you explain the following line?

; Check if this element is greater than the next element

Thank you.

[offtopic]

Argh... Maybe it's just because I'm new to programming/scripting.

And I don't use the dictionary much. My native language is Dutch.

But I prefer to talk English. It's a nice lanuage. Spanish too.

[/offtopic]

Link to comment
Share on other sites

  • Developers

@SlimShady, Buble sort keeps on going true the array comparing the current and next element in the array. If the current element is greater than the next element then swap them.

You keep on doing this exercise till there is nothing to swap anymore.

Let me know when you need a Dutch explanation...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I'm sorry. How's it compared? How's one value greater than another?

In my first post I made each value (line/word) lowercase and used the ASC function to know the (ASCII) number of that value.

But I noticed that you don't use that method in the BubbleSort script.

So, again: How are the values compared?

@JdeB:

You can tell me in Dutch. I bet I will understand it better.

Thank you.

Link to comment
Share on other sites

  • Developers

This bubble sort routine works case insensitive because the If $a < $b Then is comparing case insensitive.

Try this:

Dim $A[2]
$A[0] = "BBB"
$A[1] = "aaa"
If $A[0] > $A[1] then
   $temp = $A[0]
   $A[0] = $A[1]
   $A[1] = $temp
endif
msgbox(0,'test',$A[0] & @lf &$A[1])
You see that the aaa will show before BBB although in the ascii table a = 97 and B = 66.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 1 month later...

do you want to sort the array ?

here's a udf that can SHELL Sort multi dimentional arrays..

I like the look of this cos I'm trying to sort a two-dimensional array (basically just a list of data, with a number for each entry).

What are the three parameters supposed to be on this function?

Link to comment
Share on other sites

  • 1 month later...

I had to make one small change to get this to work....

Else; Here are the guts of it
     Do 
        $swapped = 0
        For $i = 0 To UBound($array) - 2
           If $array[$i] > $array[$i+1] Then

....I think it should read....

Else; Here are the guts of it
     Do 
        $swapped = 0
        For $i = 1 To UBound($array) - 2
           If $array[$i] > $array[$i+1] Then

By changing this to a 1 my resulting array contains this:

Dictionary, Downloads, Favorites, Filesharing, Programming, Tweaks

instead of this:

Downloads, Favorites, Filesharing, Programming, Tweaks, 6

Cool script none the less, thanks!

Scooby

Link to comment
Share on other sites

  • Developers

I had to make one small change to get this to work....

Else; Here are the guts of it
     Do 
        $swapped = 0
        For $i = 0 To UBound($array) - 2
           If $array[$i] > $array[$i+1] Then

....I think it should read....

Else; Here are the guts of it
     Do 
        $swapped = 0
        For $i = 1 To UBound($array) - 2
           If $array[$i] > $array[$i+1] Then

By changing this to a 1 my resulting array contains this:

Dictionary, Downloads, Favorites, Filesharing, Programming, Tweaks

instead of this:

Downloads, Favorites, Filesharing, Programming, Tweaks, 6

Cool script none the less, thanks!

Scooby

<{POST_SNAPBACK}>

Normally the Array starts at 0 with its first value unless you created it with a stringsplit(). It that case the 0 item will contain the UBOUND value of the array.

So you are right for arrays created with stringsplit.

:ph34r:

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

All I was stating was that the code that Nutster posted had a flaw in it...nothing more and nothing less.... and I wanted to point it out in case someone else dug it up out of the forum archives to use it. If my changing of that value is wrong in this instance then what would I change for the script to return the correct results?

Scooby

Edited by Scooby
Link to comment
Share on other sites

My script was for general arrays. Yours handled ones created with StringSplit. That is fine. Both work fine for the purposes for which they were created. :ph34r:

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...