digitalmars.D - Sorting not happpening ... ???
- Matthew (22/22) Mar 06 2005 I'm trying to sort an array of Fields (Field has opCmp defined), but .so...
- zwang (6/17) Mar 06 2005 Define the opCmp like this:
- zwang (5/16) Mar 06 2005 This is one of the pending peeves:
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (6/83) Mar 06 2005 Did you define it as: int opCmp(Object o) ?
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (4/22) Mar 06 2005 Well, you know what I mean :-)
- Matthew (7/12) Mar 06 2005 Er, no I didn't. Alas, I assumed that D was smarter in this regard than ...
- John Demme (5/31) Mar 07 2005 What do you mean? I'm not sure about .NET, but one of the things I like...
- xs0 (7/14) Mar 07 2005 If I get it correctly, D has this done exactly the same as Java - if you...
- bearophile (9/9) Dec 14 2009 Matthew:
- bearophile (1/1) Dec 14 2009 Ignore this, it's caused by the online interface, sorry.
I'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called. Am I giving .sort way too much credit? Is someone about to enlighten me that the implementation of sort does not call opCmp on objects, for arrays of references? Please advise ... Cheers -- Matthew Wilson Author: "Imperfect C++", Addison-Wesley, 2004 (http://www.imperfectcplusplus.com) Contributing editor, C/C++ Users Journal (http://www.synesis.com.au/articles.html#columns) Director, Synesis Software (www.synesis.com.au) STLSoft moderator (http://www.stlsoft.org) Synesis Software Pty Ltd P.O.Box 125 Waverley New South Wales, 2024 Australia -----------------------------------------------------
Mar 06 2005
Define the opCmp like this: int opCmp(Object o){ Field f = cast(Field)o; //do comparison here } Matthew wrote:I'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called. Am I giving .sort way too much credit? Is someone about to enlighten me that the implementation of sort does not call opCmp on objects, for arrays of references? Please advise ... Cheers
Mar 06 2005
This is one of the pending peeves: http://www.prowiki.org/wiki4d/wiki.cgi?PendingPeeves http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/74 http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/5406 Matthew wrote:I'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called. Am I giving .sort way too much credit? Is someone about to enlighten me that the implementation of sort does not call opCmp on objects, for arrays of references? Please advise ... Cheers
Mar 06 2005
Matthew wrote:I'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called.Did you define it as: int opCmp(Object o) ? "Object", same as: int opEquals(Object o);Am I giving .sort way too much credit? Is someone about to enlighten me that the implementation of sort does not call opCmp on objects, for arrays of references?It seems to be working: (just like in Java)import std.string; import std.stdio; class Field { public: this(char[] name) { m_name = name.dup; } char[] toString() { return "Field " ~ name(); } char[] name() { return m_name; } int opCmp(Object o) { if (this is o) return 0; Field field = cast(Field) o; if (field is null) assert(0); return compare(field); } int compare(Field field) { return std.string.cmp(this.name, field.name); } private: char[] m_name; unittest { Field[] fields = new Field[3]; fields[0] = new Field("cepa"); fields[1] = new Field("bepa"); fields[2] = new Field("apa"); debug writefln("BEFORE:"); foreach (int i, Field f; fields) debug writefln("%d %s", i, f); fields.sort; debug writefln("AFTER:"); foreach (int i, Field f; fields) debug writefln("%d %s", i, f); assert(fields[0] < fields[1]); assert(fields[1] < fields[2]); } } version(MAIN) { int main() { return 0; } }dmd -unittest -version=MAIN -debug sort.dBEFORE: 0 Field fepa 1 Field bepa 2 Field apa AFTER: 0 Field fepa 1 Field bepa 2 Field apa--anders
Mar 06 2005
I wrote, too soon:dmd -unittest -version=MAIN -debug sort.dOops, wrong test run output...BEFORE: 0 Field fepa 1 Field bepa 2 Field apa AFTER: 0 Field fepa 1 Field bepa 2 Field apaBEFORE: 0 Field cepa 1 Field bepa 2 Field apa AFTER: 0 Field apa 1 Field bepa 2 Field cepaWell, you know what I mean :-) --anders
Mar 06 2005
Wow! Thanks for all that effort. :-) "Anders F Björklund" <afb algonet.se> wrote in message news:d0h0h7$1p30$1 digitaldaemon.com...Matthew wrote:Er, no I didn't. Alas, I assumed that D was smarter in this regard than that disgusting Java/.NET hacky shite. Very disappointing. But thanks for the info, anyhow. :-) Cheers MatthewI'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called.Did you define it as: int opCmp(Object o) ? "Object", same as: int opEquals(Object o);
Mar 06 2005
Matthew wrote:Wow! Thanks for all that effort. :-) "Anders F Björklund" <afb algonet.se> wrote in message news:d0h0h7$1p30$1 digitaldaemon.com...What do you mean? I'm not sure about .NET, but one of the things I like about Java is it's dynamic dispatch, so it handles stuff like this "correctly". I'm hoping that D gets this soon as well. JohnMatthew wrote:Er, no I didn't. Alas, I assumed that D was smarter in this regard than that disgusting Java/.NET hacky shite.I'm trying to sort an array of Fields (Field has opCmp defined), but .sort does nothing (and Field.opCmp) is never called.Did you define it as: int opCmp(Object o) ? "Object", same as: int opEquals(Object o);Very disappointing. But thanks for the info, anyhow. :-) Cheers Matthew
Mar 07 2005
If I get it correctly, D has this done exactly the same as Java - if you use Arrays.sort() you need to implement Comparable, which defines compareTo(Object), not a type-specific version. My guess is that D's .sort is also implemented in a similar manner. If you define opCmp(Field), you don't override/implement that particular method, so it can't work.. xs0Er, no I didn't. Alas, I assumed that D was smarter in this regard than that disgusting Java/.NET hacky shite.What do you mean? I'm not sure about .NET, but one of the things I like about Java is it's dynamic dispatch, so it handles stuff like this "correctly". I'm hoping that D gets this soon as well.
Mar 07 2005
Matthew: A cleaned up case: struct B { A a; } static if(true) struct A {} void main() {} I think this may be a forward reference bug... Bye, bearophile
Dec 14 2009
Ignore this, it's caused by the online interface, sorry.
Dec 14 2009