So, I had worked out this cool workaround for the Comparable interface only handling a basic comparison between objects of a class and I was stoked to talk about it. Then yesterday I read about how the Comparator interface is supposed to be used.

Sigh. I guess this is why we read the API, huh?

See, originally I was trying to figure out how to sort objects (in the Nemesis Ladder) using different attributes derived from Members of the Ladder. I used enumerated types to describe sorting methods.

I had the class store the “current” sort method and defined a “default” sort method (its natural order) and whenever I wanted a different sort method, I changed the enumerated sort method and ran a basic sort.

Yeah, so I wasn’t 100% satisfied with that method and it compelled me to eventually look up the difference between Comparable and Comparator. Which brings us to this post. My understanding is only at “huh, that seems better.”

Comparable is supposed to be short and sweet. Return the natural ordering of two objects of the same class.

Comparator requires a little more setup, and you’re supposed to create a new class (creatively named along the lines of “NameCompare”, “DateCompare”, “AgeCompare”, or whatever) for each comparison you might want to make between the objects.

Honestly, after what I read, I’ve been thinking of different ways to implement comparators.

For starters, I hard-coded arrays of “best weapons” for each of the combat roles: striker, defender, leader, and controller. It was necessary at the time because I couldn’t think of a better way to do it.

Now that I think about it though, it would be a lot more effective, once magic items are in play, to have an inventory sort itself by the best available equipment. Providing multiple possible sort methods, or cascading sort methods would be helpful.

But that’s at the far end of things.

Much closer to the core of the program, as weapon or armor types are added or removed from the application, it would be handy if I didn’t have to rewrite the “best items” code and instead had the program figure out which were the best of what was available.

That would require me to figure out how best to determine striker, defender, leader, and controller weapons and armor. Which I’m actually okay with. This should be a fun challenge. Let’s go!