digitalmars.D - Sorting not happpening ... ???
- "Matthew" <admin.hat stlsoft.dot.org> Mar 06 2005
- zwang <nehzgnaw gmail.com> Mar 06 2005
- zwang <nehzgnaw gmail.com> Mar 06 2005
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> Mar 06 2005
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> Mar 06 2005
- "Matthew" <admin.hat stlsoft.dot.org> Mar 06 2005
- John Demme <me teqdruid.com> Mar 07 2005
- xs0 <xs0 xs0.com> Mar 07 2005
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 digitalmars.D.bugs/74 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.dBEFORE: 0 Field fepa 1 Field bepa 2 Field apa AFTER: 0 Field fepa 1 Field bepa 2 Field apa
Oops, wrong test run output...BEFORE: 0 Field cepa 1 Field bepa 2 Field apa AFTER: 0 Field apa 1 Field bepa 2 Field cepa
Well, 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: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);
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 Matthew
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...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);
Er, 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. JohnVery disappointing. But thanks for the info, anyhow. :-) Cheers Matthew
Mar 07 2005
Er, 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.
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.. xs0
Mar 07 2005









zwang <nehzgnaw gmail.com> 