TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: Peter Lavender
from: Darryl Luff
date: 1996-04-22 20:32:00
subject: Sorting an array?

>   DL>     Hi Peter,

 > Hey there.. hows things.  That 286 I got is still in
 > pieces... I might get round to setting it up on day..
 > :)

Got more stuff going soon, mainly just cases etc though, and a few old xt
& 286 motherboards. Anyway...

 > Thanks for that.  This is indeed what I ended up
 > having to do.  The sort works as it should, I was just
 > hoping to cut down on what really is redundant code,
 > since all I did was cut and paste the sorting code and
 > change the array to sort.

Yep, unfortunately that's all you can do. Unless all the fields are of the
same type. Dare I say it, but this is one thing that the dreaded 'c' allows
that Pascal doesn't, you can create a template that can work with any data
type. I can't remember the syntax, but if the code for the routines is
identical except for the type declarations, you write a function template
with dummy parameters:

#include 

template  int cmp(T x, T y)
{
  if (x > y)
    return 1;
  else if (x < y)
    return -1;
  else
    return 0;
}

void main()
{
 printf("1, 0 : %d\n", cmp(1, 0));
 printf("0, 0 : %d\n", cmp(0, 0));
 printf("0, 1 : %d\n", cmp(0, 1));

 printf("1.0, 0.0 : %d\n", cmp(1.0, 0.0));
 printf("0.0, 0.0 : %d\n", cmp(0.0, 0.0));
 printf("0.0, 1.0 : %d\n", cmp(0.0, 1.0));
}

Or something like that. The compiler substitutes the actual type you pass
into all the places 'T' appears.

Pity Pascal doesn't have it, it looks useful. Not sure if it'll
work with strings though. It would in Pascal, as you can compare
strings using the same operators as you compare numbers.

Speed/2 Pascal would be able to do it, as it has macros you could use to
achieve the same thing.

 > It also means that if I add more info, say arrival and
 > depart times and I want to do a sort on that, I now
 > have to add the code _again_.  Pity it couldn't be
 > done in the manner I had tried.  That way all that
 > needs to be done is a a few lines of extra code rather
 > than a few screens of code.

Have you tried a sorted collection, they make it nice and 'object
oriented'. You just need to add to your 'Compare' method for each new
field.

You could just write a sort routine for each data type, and call it with a
parameter telling it which field you want to use as a key (sorry, I've
forgotten your original field names):

CONST
  srtFLTNUM    = 0; { sort by flight number (integer) }
  srtSEAT      = 1; { sort by seat number (integer) }
  srtNAME      = 2; { sort by name (string) }
  srtBREAKFAST = 3; { sort by what they had for breakfast (string) }


PROCEDURE Sort(key : Integer);
BEGIN
  CASE key OF
    srtFLTNUM,
    srtSEAT      : IntegerSort(key);
    srtNAME,
    srtBREAKFAST : StringSort(key);
  END
END;

And then at least you only have to write a different sort
routine for each type.

cya, Darryl.

--- FMail 0.96â
* Origin: Point Blank - Hoppers Crossing Vic (3:632/506.4)
SEEN-BY: 633/267 270
@PATH: 632/506 998 635/503 50/99 635/544 727 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.