小编Mik*_*e N的帖子

在java中,如何在发送给我的Field变量上对泛型类T进行排序

// this function can be called if the objects sent is Comparable (they have
// already chosen the field and how it compares)
public void mySort(Object [] obj) {
     Arrays.sort(obj, null);     // this sorts based on compareTo() 
                                 // method of comparable object
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何通过Field字段更改以下函数来对数组进行排序.我已经尝试了许多不同的方法来实现它,只需将代码放在下面,代表我想要做的事情.

// this function should be used to sort a generic array of T objects, to be
// compared on the Field field
public static <T> void mySort2(T [] obj, Field field) {
    Collections.sort(obj, new Comparator<T>(){
          public …
Run Code Online (Sandbox Code Playgroud)

java sorting generics field

4
推荐指数
1
解决办法
570
查看次数

标签 统计

field ×1

generics ×1

java ×1

sorting ×1