Improved sorting

This commit is contained in:
boy0001 2015-03-11 21:27:30 +11:00
parent 0c3b084f73
commit d5fe805d8f
2 changed files with 24 additions and 16 deletions

View File

@ -149,7 +149,21 @@ public class PlotSquared {
Collections.sort(newPlots, new Comparator<Plot>() { Collections.sort(newPlots, new Comparator<Plot>() {
@Override @Override
public int compare(Plot p1, Plot p2) { public int compare(Plot p1, Plot p2) {
return p1.hashCode() + p1.world.hashCode() - p2.hashCode() + p2.world.hashCode(); int h1 = p1.hashCode();
int h2 = p2.hashCode();
if (h1 < 0) {
h1 = -h1*2 - 1;
}
else {
h1*=2;
}
if (h2 < 0) {
h2 = -h2*2 - 1;
}
else {
h2*=2;
}
return h1-h2;
} }
}); });
return newPlots; return newPlots;
@ -157,21 +171,15 @@ public class PlotSquared {
public static ArrayList<Plot> sortPlots(Collection<Plot> plots, final String priorityWorld) { public static ArrayList<Plot> sortPlots(Collection<Plot> plots, final String priorityWorld) {
ArrayList<Plot> newPlots = new ArrayList<>(); ArrayList<Plot> newPlots = new ArrayList<>();
newPlots.addAll(plots); HashMap<PlotId, Plot> worldPlots = PlotSquared.plots.get(priorityWorld);
Collections.sort(newPlots, new Comparator<Plot>() { if (worldPlots != null) {
@Override newPlots.addAll(sortPlots(worldPlots.values()));
public int compare(Plot p1, Plot p2) { }
int w1 = 0; for (Entry<String, HashMap<PlotId, Plot>> entry : PlotSquared.plots.entrySet()) {
int w2 = 0; if (!entry.getKey().equals(priorityWorld)) {
if (!p1.world.equals(priorityWorld)) { entry.getValue().values();
w1 = p1.hashCode();
}
if (!p2.world.equals(priorityWorld)) {
w2 = p2.hashCode();
}
return p1.hashCode() + w1 - p2.hashCode() - w2;
} }
}); }
return newPlots; return newPlots;
} }

View File

@ -92,7 +92,7 @@ public class Purge extends SubCommand {
return false; return false;
} }
final String worldname = args[1]; final String worldname = args[1];
if (!BlockManager.manager.isWorld(worldname) || !PlotSquared.isPlotWorld(worldname)) { if (!PlotSquared.getAllPlotsRaw().containsKey(worldname)) {
MainUtil.sendMessage(plr, "INVALID WORLD"); MainUtil.sendMessage(plr, "INVALID WORLD");
return false; return false;
} }