|
Valint Wanderer
Joined: 12 Nov 2003 Posts: 70 Location: USA
|
Posted: Thu Dec 04, 2003 10:27 pm
Sorting woes (numbers and databases) |
Occasionally, I will want to sort numbers as numbers, rather than alphabetically as text, e.g., I want the sorted list to be 1|3|10 not 1|10|3. While I created an alias to do so, this seems a bit clunky to me; is there an easier way to do this?
#ALIAS sortnumlist {
#LOOP 1,%numitems( @{%1}) {
#IF (%isnumber( %item( @{%1}, %i))) {#VAR %1 %replaceitem( %repeat( "0", 8-%len( %item( @{%1}, %i)))%item( @{%1}, %i), %i, @{%1})}}
#VAR %1 %sort( @{%1})
#LOOP 1,%numitems( @{%1}) {
#IF (%isnumber( %item( @{%1}, %i))) {#VAR %1 %replaceitem( %eval( %item( @{%1}, %i)), %i, @{%1})}}}
Also I have an occasional need (well, more just to clean up the way it looks) to sort a database record, either by %key or (for those databases with unique values as well) %val. Again, I created an alias to do so, but I'm curious as to whether there's an easier way.
#ALIAS sortdb {
#VAR sortdbsourcedb @{%1}
#VAR sortdbkeylist %null
#LOOPDB @sortdbsourcedb {#ADDITEM sortdbkeylist {%key}}
#VAR sortdbkeylist %sort(@sortdbkeylist)
#VAR %1 %null
#FORALL @sortdbkeylist {#ADDKEY %i {%i} {%db( @sortdbsourcedb, "%i")}}}
#ALIAS revsortdb {
#VAR revsorttempdb %null
#LOOPDB @{%1} {#ADDKEY revsorttempdb {%val} {%key}}
#IF (%numkeys( @{%1})!=%numkeys( @revsorttempdb)) {
#SAY Error! Not all values in source variable are unique.} {
sortdb revsorttempdb
#VAR %1 %null
#LOOPDB @revsorttempdb {#ADDKEY %1 {%val} {%key}}}} |
|
|
|
LightBulb MASTER
Joined: 28 Nov 2000 Posts: 4817 Location: USA
|
Posted: Fri Dec 05, 2003 2:43 am |
No. Your aliases might possibly be improved (I didn't evaluate them) but there's no built-in method, so there isn't any easier way than doing it yourself.
|
|
|
|
megamog75 Enchanter
Joined: 20 Nov 2002 Posts: 627 Location: USA
|
Posted: Mon Dec 08, 2003 6:13 pm |
That's great, Here is the one I use.
#ALIAS ReOrg {ReOrgN=%1;#var ReOrg %additem(%min(%replace(@{@ReOrgN},"|",",")),@ReOrg);#deli @ReOrgN %min(%replace(@{@ReOrgN},"|",","));#if @{@ReOrgN}="" {#forall @ReOrg {#say %i};#var @ReOrgN @ReOrg;ReOrg=""} {ReOrg @ReOrgN}} |
|
|
|
sp000n Novice
Joined: 04 Jul 2001 Posts: 32
|
Posted: Tue Dec 23, 2003 9:17 pm |
Valint, is your alias actually working? It doesn't seem to for me. megamog75's crashes Zmud every time (b6.66).
|
|
|
|
Talahaski Enchanter
Joined: 10 Oct 2000 Posts: 656 Location: USA
|
Posted: Tue Dec 23, 2003 9:56 pm |
If you know your max value, you could always just add that many leading zeros then do a character sort.
1,10,3 becomes 0000001,0000010,0000003 which will sort correctly to 0000001,0000003,0000010.
I'm not sure if this syntax will work. I use this stragegy when working with SQL, but the code should be fairly simple in zmud.
Something like this should pad the zeros in front?
@var=%right(%eval('000000'+@var),6) |
|
|
|
Valint Wanderer
Joined: 12 Nov 2003 Posts: 70 Location: USA
|
Posted: Tue Dec 30, 2003 8:54 pm |
> Valint, is your alias actually working?
Yes. The alias does, however, have some added linefeeds for readability, so you will have to edit it slightly to remove those extra spaces before cutting and pasting. |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|