我需要通用比较两个原始(数字!(类型(都框为对象(以找到更大的类型。我不能使用泛型,因为我只获取对象,但我知道未装箱的值是原始数字(整数、短整型、长整型、浮点数等(,所以我可以转换为 IComparable。
我该如何比较这些?CompareTo(( 抛出错误,因为它们是不同的类型,但 ChangeType 可能会导致溢出异常...?
public static int Compare(object value1, object value2)
{
//e.g. value1 = (object)(int)1; value2 = (object)(float)2.0f
if (value1 is IComparable && value2 is IComparable)
{
return (value1 as IComparable).CompareTo(value2);
//throws exception bc value1.GetType() != value2.GetType()
}
throw new ArgumentException();
}
也许是这样
public static int Compare(object value1, object value2)
{
if (value1 is double || value2 is double)
{
double d1 = Convert.ToDouble(value1);
double d2 = Convert.ToDouble(value2);
return d1.CompareTo(d2);
}
if (value1 is float || value2 is float)
{
float f1 = Convert.ToSingle(value1);
float f2 = Convert.ToSingle(value2);
return f1.CompareTo(f2);
}
long x1 = Convert.ToInt64(value1);
long x2 = Convert.ToInt64(value2);
return x1.CompareTo(x2);
}
字节、短、整型类型可以转换为长整型,而不会损失精度。
这是另一种方式:
public void test() {
Object o1 = 5465.0;
Object o2 = 5465;
Object o3 = 5465l;
Object o4 = "5465";
System.out.println(compare(o1, o2));
System.out.println(compare(o1, o3));
System.out.println(compare(o2, o3));
System.out.println(compare(o3, o4));
}
public Double convertNumber(Object o) {
if (o instanceof Double) {
return (Double) o;
} else if (o instanceof Long) {
return ((Long) o).doubleValue();
} else if (o instanceof Integer) {
return ((Integer) o).doubleValue();
} else if (o instanceof String) {
try {
return Double.parseDouble((String) o);
} catch (Exception e) {
}
}
return null;
}
public int compare(Object o1, Object o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 == null || o2 == null)
return -1;
Double asNumber1 = convertNumber(o1);
if (asNumber1 == null)
return String.valueOf(o1).compareTo(String.valueOf(o2));
Double asNumber2 = convertNumber(o2);
return asNumber2 == null ? -1 : asNumber1.compareTo(asNumber2);
}