#78072 - 2001-07-06 02:20 PM
SortArray() : My function No. 34 (but the first published)
|
Jochen
KiX Supporter
   
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
Shawn , I hope you don't mind that i took your bubble function and did a slight rework on it ... SortArray.udf :code:
;FUNCTION SortArray() ; ;AUTHOR Jochen Polster (jochen.polster@gmx.net) ; based on Shawn Tassies BubbleUp() UDF ; ;ACTION Sorts the specified Array in the specified Sort order. ; ;SYNTAX SortArray(array,SortOrder,[SortReference],[HeaderRow]) ; ;PARAMETERS array (Required) ; - A Variable representing the array to be sorted ; ; SortOrder (Required) ; - An Integer Value representing the Order the ; Array has to be sorted . ; 1 = ascending ; 2 = descending ; ; SortReference (Optional) ; - An Integer Value representing the column of a 2 dimensional ; Array. Must be referred as Arrayelement + 1 : ; $array[n][0] is column 1 !!! ; ; HeaderRow (Optional) ; - If assigned (1) the first 'Row' of a 2 dimensional Array is ; ommited. $array[0] will not get into the Sorting routine ! ; ;REMARKS Supports 2 dimensional Arrays (sorting by column). ; Will work also combined Integer/String Arrays, even mixed Values("2a"). ; If the specified Variable isn't refering to an Array or there is an ; invalid sort Order specified the Function will set @error to 1 . ; ;RETURNS The Array specified , sorted ! ; ;DEPENDENCIES None ; ;EXAMPLES break on cls ; call "path\SortArray.udf" ; $array = 300,"abc","2ab",3,"*",45,"2ac","!","2ad","xxx",76 ; $array = SortArray($array,1) ;ascending ; for each $element in $array ; $element ? ; next ; get $ ; ; 2 dimensional Array : ; ; break on cls ; call "path\SortArray.udf" ; dim $array[5] ; $array[0] = "Location","Name","Member No." ; $array[1] = "Stuttgart","jpols",611 ; $array[2] = "Houston","Bryce",600 ; $array[3] = "Toronto","Shawn",173 ; $array[4] = "Brisbane","cj",677 ; $array[5] = "Jyvaeskylae","Lonkero",2306 ; $array = SortArray($array,1,3,1) ;ascending by Member No.($array[x][2]),'header row' excluded ; for $rc = 0 to ubound($array) ; for $col = 0 to ubound($array[$rc]) ; at($rc*2,$col*15)$array[$rc][$col] ; next ; next ; get $ function SortArray($array,$Order,optional $Ref,optional $header) if ubound($array) dim $Operator select case $Order = 1 $Operator = ">" case $Order = 2 $Operator = "<" case 1 exit(1) endselect if $Ref if vartype($array[1][$ref-1]) dim $int[ubound($array)] dim $str[ubound($array)] dim $h,$i,$j,$str_count,$int_count dim $offset if $header $offset = 1 else $offset = 0 endif for $h = $offset to ubound($array) for $i = $offset to ubound($array) - 1 $ = execute("if $array[$i][$Ref-1] $Operator $array[$i+1][$Ref-1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $array[$i+1] $array[$i+1] = $array[$i] $array[$i] = $j endif next next for $i = $offset to ubound($array) if val($array[$i][$Ref-1]) or $array[$i][$Ref-1] = 0 if $array[$i][$Ref-1] = val($array[$i][$Ref-1]) $int[$int_count] = $array[$i] $int_count = $int_count + 1 else $str[$str_count] = $array[$i] $str_count = $str_count + 1 endif else $str[$str_count] = $array[$i] $str_count = $str_count + 1 endif next if $int_count and $str_count for $h = 0 to $str_count - 1 for $i = 0 to $str_count - 2 $ = execute("if $str[$i] $Operator $str[$i+1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $str[$i+1] $str[$i+1] = $str[$i] $str[$i] = $j endif next next if $Order = 1 for $i = 0 to $int_count $array[$i] = $int[$i] next for $i = $int_count to $int_count + $str_count - 1 $array[$i] = $str[$i-$int_count] next else for $h = 0 to $int_count - 1 for $i = 0 to $int_count - 2 $ = execute("if $int[$i] $Operator $int[$i+1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $int[$i+1] $int[$i+1] = $int[$i] $int[$i] = $j endif next next for $i = 0 to $str_count $array[$i] = $str[$i] next for $i = $str_count to $str_count + $int_count - 1 $array[$i] = $int[$i-$str_count] next endif endif $SortArray = $array return else exit(1) endif endif dim $int[ubound($array)] dim $str[ubound($array)] dim $h,$i,$j,$str_count,$int_count for $h = 0 to ubound($array) for $i = 0 to ubound($array) - 1 $ = execute("if $array[$i] $Operator $array[$i+1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $array[$i+1] $array[$i+1] = $array[$i] $array[$i] = $j endif next next for $i = 0 to ubound($array) if val($array[$i]) or $array[$i] = 0 if $array[$i] = val($array[$i]) $int[$int_count] = $array[$i] $int_count = $int_count + 1 else $str[$str_count] = $array[$i] $str_count = $str_count + 1 endif else $str[$str_count] = $array[$i] $str_count = $str_count + 1 endif next if $int_count and $str_count for $h = 0 to $str_count - 1 for $i = 0 to $str_count - 2 $ = execute("if $str[$i] $Operator $str[$i+1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $str[$i+1] $str[$i+1] = $str[$i] $str[$i] = $j endif next next if $Order = 1 for $i = 0 to $int_count $array[$i] = $int[$i] next for $i = $int_count to $int_count + $str_count - 1 $array[$i] = $str[$i-$int_count] next else for $h = 0 to $int_count - 1 for $i = 0 to $int_count - 2 $ = execute("if $int[$i] $Operator $int[$i+1] $$rc = 1 else $$rc = 0 endif") if $rc $j = $int[$i+1] $int[$i+1] = $int[$i] $int[$i] = $j endif next next for $i = 0 to $str_count $array[$i] = $str[$i] next for $i = $str_count to $str_count + $int_count - 1 $array[$i] = $int[$i-$str_count] next endif endif $SortArray = $array else exit(1) endif endfunction
Jochen
ps: someone might find this useful 
[ 19 July 2001: Message edited by: jpols ]
_________________________
|
Top
|
|
|
|
#78080 - 2001-07-08 02:33 AM
Re: SortArray() : My function No. 34 (but the first published)
|
Anonymous
Anonymous
Unregistered
|
What are the differences between this and the ASORT() UDF that was published on ScriptLogic's Function Library (04-Apr-2001)?If this one has more functionalty, please submit it to ScriptLogic as a replacement for ASORT(). The goal is to have a one-stop place for all the best UDF's written by the KiXtart community.... -Brian
|
Top
|
|
|
|
#78083 - 2001-07-09 10:33 AM
Re: SortArray() : My function No. 34 (but the first published)
|
Jochen
KiX Supporter
   
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
sorting 2 dimensional Arrays !finally , that was easy to implement (nearly the same as for single dimension Arrays) try this : code:
break on clscall "path\SortArray.udf" dim $array[5] $array[0] = "Stuttgart","jpols","Member No.",611 $array[1] = "Houston","Bryce","Member No.",600 $array[2] = "Toronto","Shawn","Member No.",173 $array[3] = "Brisbane","cj","Member No.",677 $array[4] = "Jyvaeskylae","Lonkero","Member No.",2306 $array = SortArray($array,1,4) ;ascending depending on Member No. ($array[x][3]) for $rc = 0 to ubound($array) for $col = 0 to ubound($array[$rc]) at($rc*2,$col*15)$array[$rc][$col] next next get $
will return (No rating by any means ) :
code:
Toronto Shawn Member No. 173Houston Bryce Member No. 600 Stuttgart jpols Member No. 611 Brisbane cj Member No. 677 Jyvaeskylae Lonkero Member No. 2306
Jochen
P.S.: Brian , are you interested in this ?
[ 09 July 2001: Message edited by: jpols ]
_________________________
|
Top
|
|
|
|
Moderator: ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
0 registered
and 323 anonymous users online.
|
|
|