Re: [CODE] Level-sorted do_who

From: Daniel A. Koepke (
Date: 04/07/99

On Tue, 6 Apr 1999, Richard P. Bandura wrote:

> My coders are attempting to order out wholist by level, higher levs
> being on top, sorting down to lower levs. We're unsure of how to do it
> really, and we're just screwing around with stuff that works,
> forgetting about efficiency (yes, i know, *shudder*). It's not my
> project, really, so I can't modify the files, but the constant
> crashing of the mud is beginnning to annoy me I'm trying
> to help out. Here it is, hacky as can be.

Ugh.  That's a headache.  I would just build a sorted array from the
linked list using qsort().

  int level_comp(const void * a, const void * b)
    struct descriptor_data * ap = (struct descriptor_data *) a;
    struct descriptor_data * bp = (struct descriptor_data *) b;
    struct char_data * char_a = ap->character;
    struct char_data * char_b = bp->character;

    /* Sorts from lowest -> highest, make this "<" to reverse */
    if (GET_LEVEL(char_a) > GET_LEVEL(char_b))
      return 1;

    return -1;


    int num_connections = 0, i;
    struct descriptor_data * d, * array;
    int (*sort_func)(const void *, const void *);

    for (d = descriptor_list; d; d = d->next)
      if (!d->connected) /* if they show-up on the who list */

    CREATE(array, struct descriptor_data, num_connections);

    for (d = descriptor_list, i = 0; d; d = d->next, i++)
      if (!d->connected) /* if they show-up on the who list */
        array[i] = *d;

    if (sort_func)
      qsort(array, num_connections, sizeof(struct descriptor_list),

    /* display who list from array[] */

This is entirely ugly mailer code, of course.  The sort_func variable is
used to point to a function we want to use to sort.  This allows us to
VERY easily change how we sort.  For instance, if we had the following
sort functions:

  int who_sort_alphabetical(const void *, const void *);
  int who_sort_alphabetical_reverse(const void *, const void *);
  int who_sort_level(const void *, const void *);
  int who_sort_level_reverse(const void *, const void *);
  int who_sort_class(const void *, const void *);
  int who_sort_class_reverse(const void *, const void *);

and we wanted to change how we sort based upon arguments given to do_who()
we might have,

  case 's': /* sort */
    char blah = *(arg + 2);
    switch (blah) {
    case 'a':
      sort_func = who_sort_alphabetical;
    case 'A':
      sort_func = who_sort_alphabetical_reverse;
    case 'l':
      sort_func = who_sort_level;
    case 'L':
      sort_func = who_sort_level_reverse;
    case 'c':
      sort_func = who_sort_class;
    case 'C':
      sort_func = who_sort_class_reverse;
      /* report usage message/error */

All of this is 100% untested.  Use at your own risk.  No need to credit
me, just please don't take discredit me (that is, don't claim you wrote


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  |

This archive was generated by hypermail 2b30 : 12/15/00 PST