help with sorting maps
Monkey Forums/Monkey Programming/help with sorting maps
| ||
I have a record class that I am using with a map:Class Record Field name :String Field score :Int Field mostBallsInRow :Int Field mostPocketed :Int Field mostCompletedSets :Int End and I extended "Map" to sort it by score: Method Compare:Int( lhs:Record,rhs:Record ) If lhs.score < rhs.score Return -1 Return lhs.score > rhs.score End Method , it does that fine but when I want to "Set" a specific record(update it or add it to the Map) it uses the Compare by score and does not find duplicate record so it fills the score table with duplicate records unless the scores match. so is it possible to use "Set" while using the custom "Compare"? |
| ||
Confused pre-coffee post removed. |
| ||
Hang on... what's your key meant to be? Your compare is using the score, but how is that going to be unique? Also, if you need to change the key, you also have to remove the item from the map and then re-add it. I think it would be easier if you described what you are trying to do, as I'm not sure that a Map is what you actually need. |
| ||
it is returning the correct values but it's not the keys that I am comparing and it seems that's the problem. |
| ||
Maybe you could use an IntMap<Record> and then use the Add(yourRecord.score, yourRecord) Method. So only a score will be added if it not already one exists. |
| ||
the problem is that more than one user can have the same score so that would be a problem. |
| ||
it's not the keys that I am comparing and it seems that's the problem Yes, that would be a problem. The compare return values cannot be correct if they aren't a comparison of the keys. |
| ||
I am trying to add players to a score table using the player as the key and the record as the value and trying to sort them by the score. I am guessing that is not possible and going to eliminate the sorting by map. and maybe go back to lists |
| ||
You'll have to use a separate list to sort by score. Monkey's List has a Compare method that can be overridden like the Map method to allow sorting by arbitrary values. |
| ||
StringMap would be for players names as keys. assign a player a unique ID to use IntMap. |
| ||
@muddy thanks. I wasn't that familiar with Maps and was hoping somebody knew something I didn't. @AdamRedwoods thanks but I think list would be more efficient for this case as I won't need to access the records randomly. and will only need one list as opposed to two maps. |
| ||
If in doubt you can always use more than one data strucure. (In my game I use a 2D array, a list and a map for my tile-objects because i can then use the one that fits the purpose best. It would be the wrong place to save ram ^^) |
| ||
Thanks Shinkiro, I'll keep that in mind. |
| ||
ah. i see. Map Set() does not return a node, so therefore, you cannot use that node reference in a List. If you could, then you add Node to List, and sort by Node.score. map set(k,v) would auto-update the Node (by reference). Yup, easier to maintain one List. |