Hi, I don't really like to do code handouts. It isn't conducive to good learning. Still, I'm vaguely interested in this since I myself have not ever sorted strings alphabetically, so I will write something up here. This implementation may have errors. I tested it on six items with various first letters and it worked great, but I may have not accounted for some edge cases, who knows. Here is the opCmp method you should use. int opCmp(players_items@ other) { // Lowercase both strings so that uppercase lettered items do not appear before lowercased ones. string other_name=string_to_lower_case(other.name); string original_name=string_to_lower_case(name); // Now simply do comparisons as we would do on ints. Keep in mind this deceptively simple action will take much longer than integer comparison. if(original_name>other_name) return 1; else if(original_name<other_name) return -1; else return 0; } Best, Aaron
On 2/19/16, Paul Lemm <paul.lem...@gmail.com> wrote: > Hi Aaron, > > Thanks for the reply that makes sense. Sorry to be a pain I'm still not > 100% > sure though how I'd add that into my code so that I could get opCmp to > sort the array of class's alphabetically by name. could I ask for an > example using the below script how you'd add that in there to sort the > array? > > > > class players_items > { > string name; > players_items(string name) > { > this.name = name; > } > } > > players_items@[] inventory; > > > many thanks > > > -----Original Message----- > From: Gamers [mailto:gamers-boun...@audyssey.org] On Behalf Of Aaron Baker > Sent: Friday, February 19, 2016 12:52 AM > To: Gamers Discussion list > Subject: Re: [Audyssey] bgt operator overloading question > > opCmp wants you to return a positive number if the source object is > greater than the parameter passed, 0 if they are equal, and -1 if the > source object is less than the parameter. > You can use the less than and greater than operator on strings, so the > rest should be obvious. One not so obvious complication is that this > sort technique is case sensative, so make sure to unify the case by > either uppercasing or lowercasing all characters. > Best, > Aaron > > On 2/18/16, Paul Lemm <paul.lem...@gmail.com> wrote: >> 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. >> > > --- > 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.