D - Doing someObjectArray.sort
- SL (35/35) Mar 25 2004 I'm trying to sort an array of SolarSystem objects, and since this isn't...
- J Anderson (5/12) Mar 25 2004 I assume you have to override opCmp which is:
- SL (8/13) Mar 25 2004 Will try. Hmm. I wonder if D has an is/instanceof operator... *if (o is
- J Anderson (6/10) Mar 25 2004 It's the same for C++ and D.
- C. Sauls (6/6) Mar 25 2004 There is an 'is' operator, but its just an alias to '===' for instance
- J Anderson (6/12) Mar 25 2004 This "is" operator is going to be confusing for C# users. I'm not
- SL (6/22) Mar 25 2004 "Converting C# to D" and "Converting Java to D" pages would probably
- J Anderson (8/15) Mar 25 2004 Good idea! The trouble is getting the man power to do such things (most...
- J C Calvarese (9/27) Mar 25 2004 There's already a spot on the wiki to put these,
- SL (9/39) Mar 26 2004 Ooooo, nice. *several minutes later* I've added (the beginnings of) a
- J C Calvarese (8/28) Mar 26 2004 Great!
- J Anderson (11/16) Mar 26 2004 Personally I find the wiki pages messy as well (particularly the first
- J C Calvarese (9/27) Mar 26 2004 I'm not sure what kind of changes you're recommending so I just made
- J Anderson (9/17) Mar 27 2004 Yes it is better. However personally I still would like less content on...
- C. Sauls (5/5) Mar 25 2004 If you're trying to do what I think you're trying to do, change the
- SL (45/51) Mar 25 2004 I just tried something like that (with a separate class) and it didn't w...
- J Anderson (15/51) Mar 25 2004 That's strange,
- J Anderson (8/20) Mar 25 2004 I should add that I didn't test for null because if you use it with
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
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
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
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 asIt'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
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
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 -Invironzsaying 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
J Anderson wrote:C. Sauls 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. -SLThere 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 -Invironzsaying it should be changed but its probably something we're going to have to explain over and over.
Mar 25 2004
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. -SLGood 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
J Anderson wrote:SL 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. -- Justin http://jcc_7.tripod.com/d/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. -SLGood 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.
Mar 25 2004
J C Calvarese wrote:J Anderson wrote: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). -SLSL 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.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. -SLGood 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.
Mar 26 2004
SL wrote: ...J C Calvarese wrote:Great!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.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
J C Calvarese wrote: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.(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.)-- -Anderson: http://badmama.com.au/~anderson/-SL
Mar 26 2004
J Anderson wrote: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. :)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(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.)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?-- Justin http://jcc_7.tripod.com/d/-SL
Mar 26 2004
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.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/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?
Mar 27 2004
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
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 -InvironzI 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
SL wrote:C. Sauls wrote: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/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 -InvironzI 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; } ---------------------------------
Mar 25 2004
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