Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 07, 2005 Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
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
-----------------------------------------------------
|
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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
>
>
|
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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 > > |
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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.d > BEFORE: > 0 Field fepa > 1 Field bepa > 2 Field apa > AFTER: > 0 Field fepa > 1 Field bepa > 2 Field apa --anders |
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | I wrote, too soon: > dmd -unittest -version=MAIN -debug sort.d > >> BEFORE: >> 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 |
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 |
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | 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. John > Very disappointing. > > But thanks for the info, anyhow. :-) > > Cheers > > Matthew > > |
March 07, 2005 Re: Sorting not happpening ... ??? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme |
>> 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
|
Copyright © 1999-2021 by the D Language Foundation