Merge pull request #9020 from Shingyx/world-hopper-sorting
Improve world hopper sorting
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,28 @@ class WorldSwitcherPanel extends PluginPanel
|
||||
switch (orderIndex)
|
||||
{
|
||||
case PING:
|
||||
return Integer.compare(r1.getPing(), r2.getPing()) * (ascendingOrder ? 1 : -1);
|
||||
// Leave worlds with unknown ping at the bottom
|
||||
return getCompareValue(r1, r2, row ->
|
||||
{
|
||||
int ping = row.getPing();
|
||||
return ping > 0 ? ping : null;
|
||||
});
|
||||
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 +203,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