www.digitalmars.com         C & C++   DMDScript  

D - Doing someObjectArray.sort

reply SL <shadowlord13 users.sourceforge.net> writes:
I'm trying to sort an array of SolarSystem objects, and since this isn't 
a hardcoded type, I need to be able to specify when one is considered > 
than the other, etc, for the sorting. I've attempted to add a 'public 
static int compare(SolarSystem s1, SolarSystem s2)' method to the 
SolarSystem class, but it isn't being called. Also tried 'public static 
int compare(void *p1, void *p2)' in SolarSystem, which isn't being 
called either.

-----
SolarSystem[] systems;
systems.length = galaxy.systems.length;
systems[] = galaxy.systems;
sortMethod=sortmode;
if (sortMethod!=0) {
	printf("Sorting.");
	systems.sort;
}
-----

sortMethod is a global variable which determines whether sorting should 
be done by total mass (a real) or star type (main sequence, red giant, 
etc - it's an int, actually).

So far it sorts the same way every time sort is called, and it doesn't 
call either of the compare methods. I haven't found anything in 
http://www.digitalmars.com/d/index.html which helped - Nothing in the 
arrays section talks about sorting, and all the "Sorting arrays" section 
of "Converting C to D" says about D is this:
----
The D Way
Sorting couldn't be easier:

	type[] array;
	...
	array.sort;		// sort array in-place
----

Hmmmmmmm. I really don't want to have to write a sort function myself, 
or have to rip one out of phobos' source and bastardize it... Hm. Does 
anyone know what I'm missing? :P
Mar 25 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
SL wrote:

 I'm trying to sort an array of SolarSystem objects, and since this 
 isn't a hardcoded type, I need to be able to specify when one is 
 considered > than the other, etc, for the sorting. I've attempted to 
 add a 'public static int compare(SolarSystem s1, SolarSystem s2)' 
 method to the SolarSystem class, but it isn't being called. Also tried 
 'public static int compare(void *p1, void *p2)' in SolarSystem, which 
 isn't being called either.
I assume you have to override opCmp which is: int *opCmp*(Object o); -- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004
parent reply SL <shadowlord13 users.sourceforge.net> writes:
J Anderson wrote:

 I assume you have to override opCmp which is:
 
 int *opCmp*(Object o);
 
 
Will try. Hmm. I wonder if D has an is/instanceof operator... *if (o is SolarSystem)* *a few minutes later* Wohoo! It works. Thanks! (Most of the time in *a few minutes later* was Java), when it turned out that there isn't one, because *cast(foo) bar*
Mar 25 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
SL wrote:

 Most of the time in *a few minutes later* was spent looking for an 

 out that there isn't one, because *cast(foo) bar* acts like *bar as 

It's the same for C++ and D. For a little more info on RTTI in D see: http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/RealtimeTypeInformation -- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004
prev sibling parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
There is an 'is' operator, but its just an alias to '===' for instance 
identity equality.  I tend to forget the cast()==null trick, thanks for 
reminding me... ;)  *runs off to simplify bunches of code that should've 
used this*

-C. Sauls
-Invironz
Mar 25 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
C. Sauls wrote:

 There is an 'is' operator, but its just an alias to '===' for instance 
 identity equality.  I tend to forget the cast()==null trick, thanks 
 for reminding me... ;)  *runs off to simplify bunches of code that 
 should've used this*

 -C. Sauls
 -Invironz
saying it should be changed but its probably something we're going to have to explain over and over. -- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004
parent reply SL <shadowlord13 users.sourceforge.net> writes:
J Anderson wrote:

 C. Sauls wrote:
 
 There is an 'is' operator, but its just an alias to '===' for instance 
 identity equality.  I tend to forget the cast()==null trick, thanks 
 for reminding me... ;)  *runs off to simplify bunches of code that 
 should've used this*

 -C. Sauls
 -Invironz
saying it should be changed but its probably something we're going to have to explain over and over.
help in that regard, much like the "Converting C to D" and "Converting C++ to D" pages do. I'm learning that those (The existing "Converting" pages) usually have good hints that aren't covered in the rest of the spec. -SL
Mar 25 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
SL wrote:

 J Anderson wrote:


 help in that regard, much like the "Converting C to D" and "Converting 
 C++ to D" pages do. I'm learning that those (The existing "Converting" 
 pages) usually have good hints that aren't covered in the rest of the 
 spec.

 -SL
Good idea! The trouble is getting the man power to do such things (most coders like to code not write). Parhaps you (or someone else) could jot down ideas for these converting pages as you (they) go along and then produce something at the end. Quite often its the people who are learning that are the best at explaining such things to newbies. -- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
J Anderson wrote:
 SL wrote:
 
 J Anderson wrote:


 help in that regard, much like the "Converting C to D" and "Converting 
 C++ to D" pages do. I'm learning that those (The existing "Converting" 
 pages) usually have good hints that aren't covered in the rest of the 
 spec.

 -SL
Good idea! The trouble is getting the man power to do such things (most coders like to code not write). Parhaps you (or someone else) could jot down ideas for these converting pages as you (they) go along and then produce something at the end. Quite often its the people who are learning that are the best at explaining such things to newbies.
There's already a spot on the wiki to put these, http://www.wikiservice.at/d/wiki.cgi?NotesForProgrammersUsedTo. I've put a little guide for BASIC users there since that's where I have the most experience. If someone wants to add to an existing page or add a new one that'd be great. -- Justin http://jcc_7.tripod.com/d/
Mar 25 2004
parent reply SL <shadowlord13 users.sourceforge.net> writes:
J C Calvarese wrote:
 J Anderson wrote:
 
 SL wrote:

 J Anderson wrote:


 help in that regard, much like the "Converting C to D" and 
 "Converting C++ to D" pages do. I'm learning that those (The existing 
 "Converting" pages) usually have good hints that aren't covered in 
 the rest of the spec.

 -SL
Good idea! The trouble is getting the man power to do such things (most coders like to code not write). Parhaps you (or someone else) could jot down ideas for these converting pages as you (they) go along and then produce something at the end. Quite often its the people who are learning that are the best at explaining such things to newbies.
There's already a spot on the wiki to put these, http://www.wikiservice.at/d/wiki.cgi?NotesForProgrammersUsedTo. I've put a little guide for BASIC users there since that's where I have the most experience. If someone wants to add to an existing page or add a new one that'd be great.
Ooooo, nice. *several minutes later* I've added (the beginnings of) a added some more stuff). And I added a note about instanceof and casting to the Java page, and noted that D allows inline assembly too. (It'd be nice if these pages were easier to find - Say, if some links on the left-bar in the spec pointed to wiki pages). -SL
Mar 26 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
SL wrote:
...
 J C Calvarese wrote:
 There's already a spot on the wiki to put these,
 http://www.wikiservice.at/d/wiki.cgi?NotesForProgrammersUsedTo.

 I've put a little guide for BASIC users there since that's where I 
 have the most experience. If someone wants to add to an existing page 
 or add a new one that'd be great.
Ooooo, nice. *several minutes later* I've added (the beginnings of) a added some more stuff).
Great!
 
 And I added a note about instanceof and casting to the Java page, and 
 noted that D allows inline assembly too.
Thanks.
 
 (It'd be nice if these pages were easier to find - Say, if some links on 
 the left-bar in the spec pointed to wiki pages).
(That sounds like a good idea to me.)
 
 -SL
-- Justin http://jcc_7.tripod.com/d/
Mar 26 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
J C Calvarese wrote:

 (It'd be nice if these pages were easier to find - Say, if some links 
 on the left-bar in the spec pointed to wiki pages).
(That sounds like a good idea to me.)
Personally I find the wiki pages messy as well (particularly the first page). I wouldn't want to fix it myself because: 1) I many offend someone 2) Not enough time to spend on this 3) I'm sure someone could do a better job at it then me I think at the very least the links should be at the bottom of the front page because links go to non-specific pages and normally users want to go somewhere specific.
 -SL
-- -Anderson: http://badmama.com.au/~anderson/
Mar 26 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
J Anderson wrote:
 J C Calvarese wrote:
 
 (It'd be nice if these pages were easier to find - Say, if some links 
 on the left-bar in the spec pointed to wiki pages).
(That sounds like a good idea to me.)
Personally I find the wiki pages messy as well (particularly the first page). I wouldn't want to fix it myself because: 1) I many offend someone 2) Not enough time to spend on this 3) I'm sure someone could do a better job at it then me
I'm not sure what kind of changes you're recommending so I just made some random changes to the front page and hopefully you would agree with me that it's an improvement. I like my changes and I'm not afraid of offending anyone. :)
 I think at the very least the links should be at the bottom of the front 
 page because links go to non-specific pages and normally users want to 
 go somewhere specific.
Are you referring to the D Website or the wiki here?
 -SL
-- Justin http://jcc_7.tripod.com/d/
Mar 26 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
J C Calvarese wrote:

 I'm not sure what kind of changes you're recommending so I just made 
 some random changes to the front page and hopefully you would agree 
 with me that it's an improvement. I like my changes and I'm not afraid 
 of offending anyone. :)
Yes it is better. However personally I still would like less content on the page. Parhaps (if possible) using some of the white space up by going horizontal with the links.
 I think at the very least the links should be at the bottom of the 
 front page because links go to non-specific pages and normally users 
 want to go somewhere specific.
Are you referring to the D Website or the wiki here?
The wiki. D links looks like my mother's shopping list (no offence meant Walter), the wiki is a little better. Imagine when we have two hundred D pages. -- -Anderson: http://badmama.com.au/~anderson/
Mar 27 2004
prev sibling parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
If you're trying to do what I think you're trying to do, change the 
prototype of that method to:
public int opCmp(SolarSystem ss)

-C. Sauls
-Invironz
Mar 25 2004
parent reply SL <shadowlord13 users.sourceforge.net> writes:
C. Sauls wrote:

 If you're trying to do what I think you're trying to do, change the 
 prototype of that method to:
 public int opCmp(SolarSystem ss)
 
 -C. Sauls
 -Invironz
I just tried something like that (with a separate class) and it didn't work: (These code snippets are for a class named StarTypeInfo) This does not sort properly: --------------------------------- int opCmp(StarTypeInfo sti) { if (amount<sti.amount) { return -1; } else if (amount>sti.amount) { return 1; } else { return 0; } } --------------------------------- This sorts fine: --------------------------------- int opCmp(Object o) { StarTypeInfo sti=cast(StarTypeInfo) o; if (sti!=null) { if (amount<sti.amount) { return -1; } else if (amount>sti.amount) { return 1; } } return 0; } --------------------------------- It is being sorted like so: --------------------------------- StarTypeInfo[] starTypes; starTypes.length=SunStagesStr.length; for (int a=0; a<SunStagesStr.length; a++) { starTypes[a]=new StarTypeInfo(a); } foreach (int i, SolarSystem ss; galaxy.systems) { if (ss!=null) { starTypes[ss.sun.stage]++; } } starTypes.sort; --------------------------------- (opPostAdd is overloaded too, it increases amount) -SL
Mar 25 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
SL wrote:

 C. Sauls wrote:

 If you're trying to do what I think you're trying to do, change the 
 prototype of that method to:
 public int opCmp(SolarSystem ss)

 -C. Sauls
 -Invironz
I just tried something like that (with a separate class) and it didn't work: (These code snippets are for a class named StarTypeInfo) This does not sort properly: --------------------------------- int opCmp(StarTypeInfo sti) { if (amount<sti.amount) { return -1; } else if (amount>sti.amount) { return 1; } else { return 0; } } --------------------------------- This sorts fine: --------------------------------- int opCmp(Object o) { StarTypeInfo sti=cast(StarTypeInfo) o; if (sti!=null) { if (amount<sti.amount) { return -1; } else if (amount>sti.amount) { return 1; } } return 0; } ---------------------------------
That's strange, Note that you can simplify that to: int opCmp(Object o) { test sti=cast(test) o; return amount - sti.amount; } but this should work (I tried it myself with no luck) int opCmp(StarTypeInfo sti) { return amount-sti.amount; } -- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
J Anderson wrote:

 That's strange,

 Note that you can simplify that to:
    int opCmp(Object o)
    {
        test sti=cast(test) o;
        return  amount - sti.amount;
    }
I should add that I didn't test for null because if you use it with another object it'll cause an error, which is what you want (then you could go back and detect that object). However the above code is seriously bad code and the following should've worked (and is what I ment in my original message).
 but this should work (I tried it myself with no luck)
 int opCmp(StarTypeInfo sti)
 {
   return amount-sti.amount;
 }
-- -Anderson: http://badmama.com.au/~anderson/
Mar 25 2004