Merge branch 'develop' into features/JAL-250_hideredundantseqs
[jalview.git] / src / jalview / util / IntRangeComparator.java
diff --git a/src/jalview/util/IntRangeComparator.java b/src/jalview/util/IntRangeComparator.java
new file mode 100644 (file)
index 0000000..cb32a0e
--- /dev/null
@@ -0,0 +1,31 @@
+package jalview.util;
+
+import java.util.Comparator;
+
+/**
+ * A comparator to order [from, to] ranges into ascending or descending order of
+ * their start position
+ */
+public class IntRangeComparator implements Comparator<int[]>
+{
+  public static final Comparator<int[]> ASCENDING = new IntRangeComparator(
+          true);
+
+  public static final Comparator<int[]> DESCENDING = new IntRangeComparator(
+          false);
+
+  boolean forwards;
+
+  IntRangeComparator(boolean forward)
+  {
+    forwards = forward;
+  }
+
+  @Override
+  public int compare(int[] o1, int[] o2)
+  {
+    int compared = Integer.compare(o1[0], o2[0]);
+    return forwards ? compared : -compared;
+  }
+
+}
\ No newline at end of file