Jump to page: 1 2
Thread overview
Doing someObjectArray.sort
Mar 25, 2004
SL
Mar 25, 2004
J Anderson
Mar 25, 2004
SL
Mar 25, 2004
J Anderson
Mar 25, 2004
C. Sauls
Mar 25, 2004
J Anderson
Mar 25, 2004
SL
Mar 25, 2004
J Anderson
Re: Converting X to D (was Doing someObjectArray.sort)
Mar 26, 2004
J C Calvarese
Mar 26, 2004
SL
Mar 26, 2004
J C Calvarese
Mar 27, 2004
J Anderson
Mar 27, 2004
J C Calvarese
Mar 27, 2004
J Anderson
Mar 25, 2004
C. Sauls
Mar 25, 2004
SL
Mar 25, 2004
J Anderson
Mar 26, 2004
J Anderson
March 25, 2004
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
March 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/
March 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 spent looking for an equivalent to C#'s 'is' operator (instanceof in Java), when it turned out that there isn't one, because *cast(foo) bar* acts like *bar as foo* in C# - it returns null if the cast is invalid)

March 25, 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
March 25, 2004
SL wrote:

> Most of the time in *a few minutes later* was spent looking for an equivalent to C#'s 'is' operator (instanceof in Java), when it turned out that there isn't one, because *cast(foo) bar* acts like *bar as foo* in C# - it returns null if the cast is invalid)

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/
March 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
March 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
> -Invironz


This "is" operator is going to be confusing for C# users.  I'm not 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/
March 25, 2004
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
> 
> 
> 
> This "is" operator is going to be confusing for C# users.  I'm not saying it should be changed but its probably something we're going to have to explain over and over.
> 

"Converting C# to D" and "Converting Java to D" pages would probably 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
March 25, 2004
SL wrote:

> J Anderson wrote:
>
> "Converting C# to D" and "Converting Java to D" pages would probably 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/
March 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
> -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
« First   ‹ Prev
1 2