@Test public void testSortDouble() { Random rand = new Random(System.currentTimeMillis()); ArrayList<Double> doubleList = new ArrayList<>(); int count = 10; for (int i = 0; i < count; i++) { doubleList.add(rand.nextDouble()); } doubleList.add(0.0); doubleList.add(0.0); doubleList.add(-1.0); doubleList.add(Double.MAX_VALUE); doubleList.add(-Double.MAX_VALUE); System.out.println("test numbers:" + doubleList); ArrayList<String> strNumList = listToStringList(doubleList); ArrayList<SelfDefineSortableKey> keyList = createKeyList(strNumList, (byte) SelfDefineSortableKey.TypeFlag.DOUBLE_FAMILY_TYPE.ordinal()); Collections.sort(keyList); ArrayList<String> strListAftereSort = new ArrayList<>(); for (SelfDefineSortableKey key : keyList) { String str = printKey(key); strListAftereSort.add(str); } assertTrue(isIncreasedOrder(strListAftereSort, new Comparator<String>() { @Override public int compare(String o1, String o2) { Double d1 = Double.parseDouble(o1); Double d2 = Double.parseDouble(o2); return d1.compareTo(d2); } })); }
@Override public int compareTo(SelfDefineSortableKey o) { if (this.typeId != o.typeId) throw new IllegalStateException("Error. Incompatible types"); if (!isNumberFamily()) { return ((Text) this.keyInObj).compareTo(((Text) o.keyInObj)); } else { if (isIntegerFamily()) { return Long.compare((Long) this.keyInObj, (Long) o.keyInObj); } else { return Double.compare((Double) this.keyInObj, (Double) o.keyInObj); } } }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { @Override public int compareTo(SelfDefineSortableKey o) { if (this.typeId != o.typeId) throw new IllegalStateException("Error. Incompatible types"); if (!isNumberFamily()) { return ((Text) this.keyInObj).compareTo(((Text) o.keyInObj)); } else { if (isIntegerFamily()) { return Long.compare((Long) this.keyInObj, (Long) o.keyInObj); } else { return Double.compare((Double) this.keyInObj, (Double) o.keyInObj); } } } }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { @Override public int compareTo(SelfDefineSortableKey o) { if (this.typeId != o.typeId) throw new IllegalStateException("Error. Incompatible types"); if (!isNumberFamily()) { return ((Text) this.keyInObj).compareTo(((Text) o.keyInObj)); } else { if (isIntegerFamily()) { return Long.compare((Long) this.keyInObj, (Long) o.keyInObj); } else { return Double.compare((Double) this.keyInObj, (Double) o.keyInObj); } } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { @Override public int compareTo(SelfDefineSortableKey o) { if (this.typeId != o.typeId) throw new IllegalStateException("Error. Incompatible types"); if (!isNumberFamily()) { return ((Text) this.keyInObj).compareTo(((Text) o.keyInObj)); } else { if (isIntegerFamily()) { return Long.compare((Long) this.keyInObj, (Long) o.keyInObj); } else { return Double.compare((Double) this.keyInObj, (Double) o.keyInObj); } } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); void init(Text key, byte typeId); void init(Text key, DataType type); @Override int compareTo(SelfDefineSortableKey o); @Override void write(DataOutput dataOutput); @Override void readFields(DataInput dataInput); short getTypeId(); Text getText(); boolean isNumberFamily(); boolean isIntegerFamily(); byte getTypeIdByDatatype(DataType type); void setTypeId(byte typeId); }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { @Override public int compareTo(SelfDefineSortableKey o) { if (this.typeId != o.typeId) throw new IllegalStateException("Error. Incompatible types"); if (!isNumberFamily()) { return ((Text) this.keyInObj).compareTo(((Text) o.keyInObj)); } else { if (isIntegerFamily()) { return Long.compare((Long) this.keyInObj, (Long) o.keyInObj); } else { return Double.compare((Double) this.keyInObj, (Double) o.keyInObj); } } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); void init(Text key, byte typeId); void init(Text key, DataType type); @Override int compareTo(SelfDefineSortableKey o); @Override void write(DataOutput dataOutput); @Override void readFields(DataInput dataInput); short getTypeId(); Text getText(); boolean isNumberFamily(); boolean isIntegerFamily(); byte getTypeIdByDatatype(DataType type); void setTypeId(byte typeId);  }