Sort worlds in world hopper using Guava
Use Guava to abstract the world hopper sorting code. This fixes secondary sorting when sorting by activity, e.g. when sorting by "Ping" and then by "Activity".
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
package net.runelite.client.plugins.worldhopper;
|
||||
|
||||
import com.google.common.collect.Ordering;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
@@ -33,6 +34,7 @@ import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import lombok.AccessLevel;
|
||||
@@ -159,24 +161,23 @@ class WorldSwitcherPanel extends PluginPanel
|
||||
switch (orderIndex)
|
||||
{
|
||||
case PING:
|
||||
return Integer.compare(r1.getPing(), r2.getPing()) * (ascendingOrder ? 1 : -1);
|
||||
return getCompareValue(r1, r2, WorldTableRow::getPing);
|
||||
case WORLD:
|
||||
return Integer.compare(r1.getWorld().getId(), r2.getWorld().getId()) * (ascendingOrder ? 1 : -1);
|
||||
return getCompareValue(r1, r2, row -> row.getWorld().getId());
|
||||
case PLAYERS:
|
||||
return Integer.compare(r1.getUpdatedPlayerCount(), r2.getUpdatedPlayerCount()) * (ascendingOrder ? 1 : -1);
|
||||
return getCompareValue(r1, r2, WorldTableRow::getUpdatedPlayerCount);
|
||||
case ACTIVITY:
|
||||
return r1.getWorld().getActivity().compareTo(r2.getWorld().getActivity()) * -1 * (ascendingOrder ? 1 : -1);
|
||||
// Leave empty activity worlds on the bottom of the list
|
||||
return getCompareValue(r1, r2, row ->
|
||||
{
|
||||
String activity = row.getWorld().getActivity();
|
||||
return !activity.equals("-") ? activity : null;
|
||||
});
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
// Leave empty activity worlds on the bottom of the list
|
||||
if (orderIndex == WorldOrder.ACTIVITY)
|
||||
{
|
||||
rows.sort((r1, r2) -> r1.getWorld().getActivity().equals("-") ? 1 : -1);
|
||||
}
|
||||
|
||||
rows.sort((r1, r2) ->
|
||||
{
|
||||
boolean b1 = plugin.isFavorite(r1.getWorld());
|
||||
@@ -197,6 +198,17 @@ class WorldSwitcherPanel extends PluginPanel
|
||||
listContainer.repaint();
|
||||
}
|
||||
|
||||
private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function<WorldTableRow, Comparable> compareByFn)
|
||||
{
|
||||
Ordering<Comparable> ordering = Ordering.natural();
|
||||
if (!ascendingOrder)
|
||||
{
|
||||
ordering = ordering.reverse();
|
||||
}
|
||||
ordering = ordering.nullsLast();
|
||||
return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2));
|
||||
}
|
||||
|
||||
void updateFavoriteMenu(int world, boolean favorite)
|
||||
{
|
||||
for (WorldTableRow row : rows)
|
||||
|
||||
Reference in New Issue
Block a user