@Test public void testSortNormalString() { int count = 10; ArrayList<String> strList = new ArrayList<>(); for (int i = 0; i < count; i++) { UUID uuid = RandomUtil.randomUUID(); strList.add(uuid.toString()); } strList.add("hello"); strList.add("hello"); strList.add("123"); strList.add(""); ArrayList<SelfDefineSortableKey> keyList = createKeyList(strList, (byte) SelfDefineSortableKey.TypeFlag.NONE_NUMERIC_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) { return o1.compareTo(o2); } })); }
@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);  }