Files
ObsidianVault/.trash/Comparable Research Discussion.md
T
2026-05-17 12:19:19 -07:00

58 lines
3.2 KiB
Markdown

https://github.com/search?q=repo%3Arunelite%2Frunelite+comparable&type=code
https://github.com/runelite/runelite/blob/e25afded97ae8c4bcc8754e60511de8563060b67/runelite-api/src/main/java/net/runelite/api/Nameable.java#L30
https://github.com/runelite/runelite/blob/e25afded97ae8c4bcc8754e60511de8563060b67/runelite-client/src/main/java/net/runelite/client/plugins/config/PluginSearch.java#L49
I chose runelite, an open-source Runescape client written in Java, as my project. Here is a link: https://github.com/mojo626/AD300-ComparableInterface
The first use of a comparator that I found was the comparator [AlphaModelComparator](https://github.com/runelite/runelite/blob/c7ebe1f362e1cd794813afc0be848d09f01f3d16/runelite-client/src/main/java/net/runelite/client/plugins/gpu/Zone.java#L496)
```
static class AlphaModelComparator implements Comparator<AlphaModel>
{
int zx, zz;
int cx, cy, cz;
@Override
public int compare(AlphaModel o1, AlphaModel o2)
{
return Integer.compare(z(o2), z(o1));
}
private int z(AlphaModel m)
{
final int mx = (m.x + ((zx - m.zofx) << 10));
final int mz = (m.z + ((zz - m.zofz) << 10));
return (mx - cx) * (mx - cx) +
(m.y - cy) * (m.y - cy) +
(mz - cz) * (mz - cz);
}
}
```
I don't know exactly how this comparator works but it is for a plugin that shifts rendering to the GPU and I assume that this is used to sort the faces of models in game to make sure that they are being rendered in the correct order. A comparator is useful here because sorting faces based on z position to the camera is difficult, especially with things such as culling or overlapping faces, and a comparator allows easy definition of a custom sorting function.
The only use of comparable that I could find was the [Namable](https://github.com/runelite/runelite/blob/c7ebe1f362e1cd794813afc0be848d09f01f3d16/runelite-api/src/main/java/net/runelite/api/Nameable.java#L30) interface.
```
/**
* Represents a chat entity that has a name.
*/
public interface Nameable extends Comparable<Nameable>
{
/**
* The name of the player.
*
* @return the name
*/
String getName();
/**
* The previous name the player had.
*
* @return the previous name
*/
String getPrevName();
}
```
Since this interface represents something in chat, it is useful to define a custom sorting function. It might be helpful to sort the entities by name, but it could also be used to sort them into something like categories first and then alphabetically within them and using a comparable allows for easy changing and modularity of the sorting function.
Comparator seems to be more useful in this project since for many of the objects there will likely be multiple ways that they can be sorted and writing several comparators allows for this. I didn't notice any particular best practices or patterns in the use of comparators or comparables since there were not very many examples in the project that I chose. For the AlphaModelComparator class it seems strange that there are local variables that are populated in the comparator after creating it and before sorting. Maybe these variables (zx, cx etc.) could be added to the objects being compared to make the code a little more readable?