Page 1 of 2 12>
Topic Options
#78072 - 2001-07-06 02:20 PM SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
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
#78073 - 2001-07-06 02:32 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
... Will recode that one to cover mixed arrays (integer and strings)

Jochen

_________________________



Top
#78074 - 2001-07-06 03:08 PM Re: SortArray() : My function No. 34 (but the first published)
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
No problem my brother.

Just as a comment - you already know that I'm not a huge execute() function fan. My feeling is that it should only be used in special circumstances. One where the power of the function can accomplish the impossible, compact code or help improve performance. You just perfectly demonstrated two of those circumstances

[ 06 July 2001: Message edited by: Shawn ]

Top
#78075 - 2001-07-06 03:47 PM Re: SortArray() : My function No. 34 (but the first published)
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
one thing I've thinking of is sorting of 2 dimensional array (thinking about tables).
in udf-calling say what array (or table) and by which field want them to be sorted...
it just needs the basic udf/function/support for little more complex arrays.
but if I remember right somebody allready pointed out howto do that... can't just remember which article it was.
but what more udf's we are going to use, that much more we/I would like to get #include to kix!
otherwise scripts are going to grow too huge to control.
_________________________
!

download KiXnet

Top
#78076 - 2001-07-06 04:47 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Lonkero :

#include : this topic
(conclusion: call "path\file.udf")

sorting 2 dimensional arrays : on the way !

My kolleague here is just trying to shoot holes in sorting mixed arrays enhancement


Jochen


P.S.: He shot one : "integer combined with letters !"

[ 06 July 2001: Message edited by: jpols ]

_________________________



Top
#78077 - 2001-07-06 05:41 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Ok, filled the holes my colleague shot ...(updated original post)

Lonkero, Shawn , anyone else:

Any comments before i go to 2 dimsenional Arrays ? I mean , do these Arrays get sorted correctly in your Opinion ???


Jochen

[ 06 July 2001: Message edited by: jpols ]

_________________________



Top
#78078 - 2001-07-06 06:53 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Thanx to Shawn,

he shot another hole in it , that is now fixed (above)

Jochen

(Should've been testing for pure string/integer Arrays after adding mixed mode )

_________________________



Top
#78079 - 2001-07-07 11:38 AM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
and another one found by Shawn ...(fixed version above)

0's were treated as strings inside the function (because of if val(etc))

so the output of 100,200,300,0,0 was
100
200
300
0
0

Jochen

_________________________



Top
#78080 - 2001-07-08 02:33 AM Re: SortArray() : My function No. 34 (but the first published)
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
#78081 - 2001-07-08 11:45 AM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Brian,

in the first moment I saw your reply , I thought : damn, why re-inventing the wheel!

But after a closer look to Asort() i saw that it sorts only integer OR string Arrays ...

My Version can also sort mixed ones in an , was it alphanumeric way ?
Rule is : Integer -> mixed integer/string (2a) -> pure string
And when finished (next week?) it'll support
2 dimensional Arrays with a sort reference (column)


Jochen

_________________________



Top
#78082 - 2001-07-08 04:26 PM Re: SortArray() : My function No. 34 (but the first published)
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
offcourse there shall be some enchangements... it is longer!

but still waiting for tables...
actually I tried to make it in my head and found out that the base sort() has to modified for full functioning...
but I'd be really glad to see you guys making a multidimensional (2) sorting and support for arrays...

by the way, it is pretty hot here... going out!

_________________________
!

download KiXnet

Top
#78083 - 2001-07-09 10:33 AM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
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 cls

call "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. 173

Houston 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
#78084 - 2001-07-09 02:48 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I COULD (that doesn't mean I want, therefore I ask ) ,
if anyone wants me to , adding support for cascaded sorts (sort by blah asc, then by blah 1 desc,etc)
and an option to exclude a header row , just like excel does ...

Any Orders ???


Jochen

[ 09 July 2001: Message edited by: jpols ]

_________________________



Top
#78085 - 2001-07-09 03:37 PM Re: SortArray() : My function No. 34 (but the first published)
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hehheh...
you are the man!
or boy... how ever,
you might make an udf that uses your own udf as base and does make a table with headers.

then adding some html-support here and there and we will have colourfull tables...
well, uttleas tables.

but you sort is complex enough to use it as "stdio.h"... I mean it can be used in lots of places from now on.

_________________________
!

download KiXnet

Top
#78086 - 2001-07-09 03:50 PM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Lonkero,

Thanks for not wanting me to do this ...
but in the mean time I thought it could be interesting to do this (it'll be so or so optional , so who cares) ...

Get back to this at the and of the week , as i have some 'Real Work' to do here


Jochen

_________________________



Top
#78087 - 2001-07-11 08:08 AM Re: SortArray() : My function No. 34 (but the first published)
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
jpols,
is this code of your out there somewhere, for download?
I can get from this board too but if you have a page/place to fit it on, would easier to check for new changes and prob for you to keep it up to date.
actually. this board does not give the lines nicely, but messes up the lines.
don't want to fix every line with hand before can use the code.

[ 11 July 2001: Message edited by: Lonkero ]

_________________________
!

download KiXnet

Top
#78088 - 2001-07-11 10:23 AM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Lonkero,

yes , a bit difficult ... before the update of the bb we had the possibility to click on edit post , then just copy and paste between
['code] and ['/code] and the formatting was preserved ... now the only possibility is to copy the code , paste it into wordpad (write.exe) , copy all and paste it then to the editor of your choice .
No , i don't have a homepage/website , but i will ask Bryce to become a member of his Script repository ...

Jochen

_________________________



Top
#78089 - 2001-07-11 11:28 AM Re: SortArray() : My function No. 34 (but the first published)
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
about the bb.
there is this nice thing that my script editor has been since ver 3.51 (if I remember right) the great NOTEPAD!
so I can't just copy and paste it, 'cause there ain't NL neither CR codes in the bb text.
so I can't get any text out of here. that's why I miss the old version of bb.
_________________________
!

download KiXnet

Top
#78090 - 2001-07-11 11:47 AM Re: SortArray() : My function No. 34 (but the first published)
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Hi Lonkero,

If you use Netscape you can copy the text right on screen and it preserves the format except after first line it will have a large indent (but still be formatted correctly) I then use UltraEdit and in block mode I hilight the whole columns and delete which then places all the code on the start on each new line and properly preserved.

Top
#78091 - 2001-07-11 11:58 AM Re: SortArray() : My function No. 34 (but the first published)
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Lonkero:

i just wanted to send you the udf file but your e-mail seems to be wrong ???
got an error message that the domain couldn't be resolved ...

Jochen


Henri : we want our old code-grabbing back !!!

[ 11 July 2001: Message edited by: jpols ]

_________________________



Top
Page 1 of 2 12>


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 323 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.178 seconds in which 0.098 seconds were spent on a total of 13 queries. Zlib compression enabled.