[comp.sys.next] How do I sort a list?

eps@toaster.SFSU.EDU (Eric P. Scott) (06/03/91)

Assume I have a list of objects, and I want to sort them
according to some arbitrary criteria.  As far as I can tell, the
least unpleasant approach is to subclass List and use poseAs:
to add a sort method:

--- SortableList.h
| #import <objc/List.h>
|
| @interface SortableList:List
| {
| }
|
| - sortUsing:(int (*)(const void *, const void *))compar;
|
| @end
---
--- SortableList.m
| #import "SortableList.h"
| #import <stdlib.h>
|
| @implementation SortableList
|
| - sortUsing:(int (*)(const void *, const void *))compar
| {
|     if (numElements>1) (void)qsort((void *)dataPtr, numElements,
|         sizeof (id), compar);
|     return self;
| }
|
|
| @end
---

(Where do I invoke the poseAs:?  appDidInit:?)

Now suppose my list of objects is the cellList of a one-
dimensional matrix.  Does that change things, and if so, how?
What if one or more cells are selected before I perform the sort?

What if the matrix is an argument to browser:fillMatrix:inColumn:?

Am I missing something obvious?  (i.e. is there a better way?)

					-=EPS=-

ernest@pundit.cithep.caltech.edu (Ernest Prabhakar) (06/04/91)

Eric P. Scott writes
> Assume I have a list of objects, and I want to sort them
> according to some arbitrary criteria.  As far as I can tell, the
> least unpleasant approach is to subclass List and use poseAs:
> to add a sort method:
> 
> 
> 					-=EPS=-

The most general way to do this would be to create a Category of List.
"List (SortableList)".  For lack of anything better, I suggest you use the  
conventions in Stepstone's classes.
Implement "rankRelativeTo:" for each object you want.
Use "qsort" (the system call) to do the sorting.
Implement "sort" as part of the category.

I did this for the "Deck" object as part of the "DeckOfCards" object suite.  If  
I did it again (which I might), I'd make Deck a category first, then perhaps a  
subclass.

In fact, has anyone tried to get the functionality of StepStone's classes on  
top of NeXT's (sorting, finding, set operations) List and Storage objects?  You  
could probably do it entirely as categories.

Even if Stepstone dies, let's keep the dream of reusable objects alive!

	-- Ernie P.
"Wherefore art thou Objective-C++?"
--
Ernest N. Prabhakar                  Caltech High Energy Physics
CaJUN President        NeXTMail:ernest@pundit.cithep.caltech.edu
"...and ourselves, your servants for Jesus sake." - II Cor 5:13a

davis@usenet.INS.CWRU.Edu (Palmer Davis) (06/04/91)

In article <1991Jun3.173343.18585@nntp-server.caltech.edu> ernest@pundit.cithep.caltech.edu (Ernest Prabhakar) writes:
>
>"Wherefore art thou Objective-C++?"
>

Try /bin/cc++.... :-)

-- PTD --


-- 
Palmer Davis <davis@po.cwru.edu>     I'm probably wrong, so don't blame INS.
CWRU Information Network Services                 Life is short.
"Where I came from, we were taught that lawyers had a high social status.
But my father never would have friends who were lawyers."  -- Lim

eps@toaster.SFSU.EDU (Eric P. Scott) (06/04/91)

In article <1991Jun3.173343.18585@nntp-server.caltech.edu>
	ernest@pundit.cithep.caltech.edu (Ernest Prabhakar) writes:
>The most general way to do this would be to create a Category of List.
>"List (SortableList)".

A rhetorical question: why can't I do this in InterfaceBuilder?
(Grr.)

					-=EPS=-

scott@mcs-server.gac.edu (Scott Hess) (06/04/91)

In article <1649@toaster.SFSU.EDU> eps@toaster.SFSU.EDU (Eric P. Scott) writes:
   In article <1991Jun3.173343.18585@nntp-server.caltech.edu>
	   ernest@pundit.cithep.caltech.edu (Ernest Prabhakar) writes:
   >The most general way to do this would be to create a Category of List.
   >"List (SortableList)".

   A rhetorical question: why can't I do this in InterfaceBuilder?
   (Grr.)

Related Q:  Why can't I do general Object manipulation in IB?
In other words, why is IB's neatness limited to objects that
the end-user can see?  It'd be neat to be able to pre-initialize
Object subclasses, and Lists, and the like . . .

Later,
--
scott hess                      scott@gac.edu
Independent NeXT Developer	Graduated GAC Undergrad!
<I still speak for nobody>
Note:  I have moved home for a time.  My email address will still be
valid.  Any SnailMail should be redirected, along with phone calls.
At the least, my parents can tell you how to get hold of me, or
forward any mail . . .
Old:	PO 829, GAC, St. Peter, MN  56082	(507) 933-8466
New:	RR#4 Box 227 Pipestone, MN  56164	(507) 825-2788