Hi Aaron and John, This is something that has had me stuck for a while now. As in a game I have written, I have created a class, and then created an aray of that class, but want to sort the array so it is alphabetical. I've read the help topic on sorting arrays which makes sense for sorting an array of strings , but I don't understand how I would get it to sort an array of classes. The BGT help document just says you need to overload the comparison operator. I've always been a little unclear on overloading operators and not sure how I would go about overloading the comparison operator in my class. So using the basic class code below how would I go about overloading the comparison operator and then sorting the array of that class alphabetically by name?
class players_items { string name; string description; bool key_item; players_items(string name, string description, bool key_item) { this.name = name; this.description = description; this.key_item = key_item; } } Players_items[] inventory; Many thanks for any help you can offer Paul -----Original Message----- From: Gamers [mailto:gamers-boun...@audyssey.org] On Behalf Of Aaron Baker Sent: Thursday, February 18, 2016 1:20 PM To: Gamers Discussion list Subject: Re: [Audyssey] bgt operator overloading question Hello, Did you actually try it? I know that that can make the difference between runtime error and no runtime error. I thought it was kind of odd too, but it is usually bad practice to pass an object by value anyway. If you're worried about the object changing, you might be able to write it as "const stat@ other". I really think that is the problem. This code gives me a runtime error: " class stat { int percent; int opCmp(stat other) { return percent-other.percent; } } stat[] stats; void main() { stats.sort_descending(); } " And this code doesn't: " class stat { int percent; int opCmp(stat@ other) { return percent-other.percent; } } stat[] stats; void main() { stats.sort_descending(); } " Best, Aaron On 2/18/16, john <jpcarnemo...@gmail.com> wrote: > Seems to be a nogo. > I'd be surprised if that was the issue; handles are close enough to objects > > for it to not matter as function parameters in my experience. > > -------------------------------------------------- > From: "Aaron Baker" <theultimatemasterofarc...@gmail.com> > Sent: Wednesday, February 17, 2016 19:51 > To: "Gamers Discussion list" <gamers@audyssey.org> > Subject: Re: [Audyssey] bgt operator overloading question > > Hey John, > Try switching your parameter from "stat other" to "stat@ other". > I think opCmp wants a handle (by reference), not a by value object. > Best, > Aaron > > On 2/17/16, john <jpcarnemo...@gmail.com> wrote: >> Hi all, >> I'm attempting to overload comparison operators in order to sort an array >> >> of >> objects, with the following function: >> //begin code >> int opCmp (stat other) >> { >> return percent-other.percent; >> } >> //end code >> However, the program gives me the following error as soon as comparison >> takes place: >> A runtime error occurred. File:function: void main()Description: Type >> 'stat' >> does not have a matching opCmp method >> >> >> >> Can anybody shed some light on this one? >> >> >> >> Thanks, >> >> John >> >> >> >> P.S: >> >> Yes, the comparison function is within the stat class, and comparison is >> being done by array.sort_descending. >> >> I have tried several case changes to the function name, and have stuck >> with >> the above because it matches both the manual and compiler error. >> --- >> Gamers mailing list __ Gamers@audyssey.org >> If you want to leave the list, send E-mail to >> gamers-unsubscr...@audyssey.org. >> You can make changes or update your subscription via the web, at >> http://audyssey.org/mailman/listinfo/gamers_audyssey.org. >> All messages are archived and can be searched and read at >> http://www.mail-archive.com/gamers@audyssey.org. >> If you have any questions or concerns regarding the management of the >> list, >> please send E-mail to gamers-ow...@audyssey.org. >> > > --- > Gamers mailing list __ Gamers@audyssey.org > If you want to leave the list, send E-mail to > gamers-unsubscr...@audyssey.org. > You can make changes or update your subscription via the web, at > http://audyssey.org/mailman/listinfo/gamers_audyssey.org. > All messages are archived and can be searched and read at > http://www.mail-archive.com/gamers@audyssey.org. > If you have any questions or concerns regarding the management of the list, > please send E-mail to gamers-ow...@audyssey.org. > > > --- > Gamers mailing list __ Gamers@audyssey.org > If you want to leave the list, send E-mail to > gamers-unsubscr...@audyssey.org. > You can make changes or update your subscription via the web, at > http://audyssey.org/mailman/listinfo/gamers_audyssey.org. > All messages are archived and can be searched and read at > http://www.mail-archive.com/gamers@audyssey.org. > If you have any questions or concerns regarding the management of the list, > please send E-mail to gamers-ow...@audyssey.org. > --- Gamers mailing list __ Gamers@audyssey.org If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org. You can make changes or update your subscription via the web, at http://audyssey.org/mailman/listinfo/gamers_audyssey.org. All messages are archived and can be searched and read at http://www.mail-archive.com/gamers@audyssey.org. If you have any questions or concerns regarding the management of the list, please send E-mail to gamers-ow...@audyssey.org. --- Gamers mailing list __ Gamers@audyssey.org If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org. You can make changes or update your subscription via the web, at http://audyssey.org/mailman/listinfo/gamers_audyssey.org. All messages are archived and can be searched and read at http://www.mail-archive.com/gamers@audyssey.org. If you have any questions or concerns regarding the management of the list, please send E-mail to gamers-ow...@audyssey.org.