help with sorting maps

Monkey Forums/Monkey Programming/help with sorting maps

Jesse(Posted 2012) [#1]
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"?


muddy_shoes(Posted 2012) [#2]
Confused pre-coffee post removed.


muddy_shoes(Posted 2012) [#3]
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.


Jesse(Posted 2012) [#4]
it is returning the correct values but it's not the keys that I am comparing and it seems that's the problem.


Shinkiro1(Posted 2012) [#5]
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.


Jesse(Posted 2012) [#6]
the problem is that more than one user can have the same score so that would be a problem.


muddy_shoes(Posted 2012) [#7]
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.


Jesse(Posted 2012) [#8]
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


muddy_shoes(Posted 2012) [#9]
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.


AdamRedwoods(Posted 2012) [#10]
StringMap would be for players names as keys.
assign a player a unique ID to use IntMap.


Jesse(Posted 2012) [#11]
@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.


Shinkiro1(Posted 2012) [#12]
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 ^^)


Jesse(Posted 2012) [#13]
Thanks Shinkiro, I'll keep that in mind.


AdamRedwoods(Posted 2012) [#14]
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.