Sorting an Array

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Jul 10 09:06:29 PDT 2007


gogamza wrote:
 >     int opCmp(keyvalue* s){
 >         char[][] paramKV = split(s.str, "-");
 >         char[][] thisKV = split(this.str, "-");
 >         return toInt(thisKV[0]) - toInt(paramKV[0]);
 >     }

That'll start going wrong once the difference between the integers 
starts to get over int.max. The safest way to return an opCmp value for 
integers and larger basic types is with things like:
---
auto val1 = toInt(thisKV[0]);
auto val2 = toInt(paramVK[0]);
return typeid(int).compare(&val1, &val2);
---
(Optionally, replace 'int' with 'typeof(val1)' in that last line if 
you're not sure the type returned by toInt will always be an int)


More information about the Digitalmars-d-learn mailing list