diff --git a/src/net/azib/ipscan/core/ScanningResultComparator.java b/src/net/azib/ipscan/core/ScanningResultComparator.java index 6872ea00..26757bbc 100644 --- a/src/net/azib/ipscan/core/ScanningResultComparator.java +++ b/src/net/azib/ipscan/core/ScanningResultComparator.java @@ -20,15 +20,16 @@ public class ScanningResultComparator implements Comparator { Object val1 = r1.getValues().get(index); Object val2 = r2.getValues().get(index); - if (val1 == val2) { - return 0; - } if (val1 == null) val1 = NotAvailable.VALUE; if (val2 == null) val2 = NotAvailable.VALUE; - + int result; + if (val1 == val2) { + result = 0; + } + else if (val1.getClass() == val2.getClass() && !(val1 instanceof String) && val1 instanceof Comparable) { // both are the same type and Comparable result = ((Comparable)val1).compareTo(val2); @@ -45,6 +46,11 @@ public class ScanningResultComparator implements Comparator { } } + if (result == 0 && index != 0) { + // if values are equal, order them according to the IPs + result = ((Comparable)r1.getValues().get(0)).compareTo(r2.getValues().get(0)); + } + return result * (ascending ? 1 : -1); } diff --git a/test/net/azib/ipscan/core/ScanningResultComparatorTest.java b/test/net/azib/ipscan/core/ScanningResultComparatorTest.java index efc8d1d2..7f415ea6 100644 --- a/test/net/azib/ipscan/core/ScanningResultComparatorTest.java +++ b/test/net/azib/ipscan/core/ScanningResultComparatorTest.java @@ -12,6 +12,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; +import net.azib.ipscan.core.values.InetAddressHolder; import net.azib.ipscan.core.values.IntegerWithUnit; import net.azib.ipscan.core.values.NotAvailable; import net.azib.ipscan.core.values.NotScanned; @@ -67,7 +68,7 @@ public class ScanningResultComparatorTest { assertTrue(comparator.compare(res("a", "z"), res("z", "a")) > 0); comparator.byIndex(2, false); - assertTrue(comparator.compare(res("a", "z", "mmm"), res("z", "a", "mmm")) == 0); + assertTrue(comparator.compare(res("a", "x", "mmm"), res("a", "y", "mmm")) == 0); } @Test @@ -89,6 +90,22 @@ public class ScanningResultComparatorTest { assertTrue(comparator.compare(res("Z"), res("a")) > 0); } + @Test + public void fallbackToIP() throws Exception { + Object ip1 = new InetAddressHolder(InetAddress.getByName("127.0.0.25")); + Object ip2 = new InetAddressHolder(InetAddress.getByName("127.0.0.30")); + Object ip3 = new InetAddressHolder(InetAddress.getByName("128.0.0.25")); + + comparator.byIndex(1, true); + assertTrue(comparator.compare(res(ip1, "a"), res(ip1, "A")) == 0); + assertTrue(comparator.compare(res(ip1, "a"), res(ip2, "a")) < 0); + assertTrue(comparator.compare(res(ip3, "a"), res(ip2, "a")) > 0); + + comparator.byIndex(1, false); + assertTrue(comparator.compare(res(ip1, null), res(ip2, null)) > 0); + assertTrue(comparator.compare(res(ip3, null), res(ip2, null)) < 0); + } + @Test public void sortingWorksInBothDirections() throws Exception { ScanningResult[] results = { @@ -121,7 +138,7 @@ public class ScanningResultComparatorTest { assertEquals(NotAvailable.VALUE, results[3].getValues().get(0)); assertEquals(NotScanned.VALUE, results[5].getValues().get(0)); } - + private ScanningResult res(Object ... values) throws UnknownHostException { ScanningResult result = new ScanningResult(InetAddress.getLocalHost(), values.length); for (int i = 0; i < values.length; i++) {