Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何根据发送给我的字段变量对泛型类T进行排序_Java_Sorting_Generics_Field - Fatal编程技术网

在java中,如何根据发送给我的字段变量对泛型类T进行排序

在java中,如何根据发送给我的字段变量对泛型类T进行排序,java,sorting,generics,field,Java,Sorting,Generics,Field,我的问题是,需要如何更改以下函数以按字段对数组进行排序。我已经尝试了许多不同的实现方法,只是在下面放了一些代码,这些代码代表了我要做的事情 // 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(ob

我的问题是,需要如何更改以下函数以按字段对数组进行排序。我已经尝试了许多不同的实现方法,只是在下面放了一些代码,这些代码代表了我要做的事情

// 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
}
//此函数应用于对T对象的通用数组进行排序,以便
//现场比较
公共静态void mySort2(T[]对象,字段){
Collections.sort(obj,newcomparator(){
公共整数比较(T obj1,T obj2)
{
返回obj1.field.compareTo(obj2.field);//这不起作用
//因为我需要输入字段名和
//不是一个变量,但我不知道是什么
//当它被送到我这里的时候,我会的
}
});
}

您需要使用以反射方式访问字段。在此之后,您将需要强制转换到
Comparable

是否确实需要传递
字段
实例并以反射方式查找字段?作为一种可能的替代方法(根据JVM实现,性能可能更高),您可以传递lambda或方法引用以访问字段:

// 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 int compare(T obj1, T obj2)
          {
             return obj1.field.compareTo(obj2.field); // this does not work
                             //  since I need to the name of the field and
                             //  not a variable, however I do not know what
                             //  T will be when it is sent to me
          }
      });
}
如果您坚持使用Java 7,您也可以做同样的事情,但它有点冗长:

Foo[] foos = ...
mySort(foos, Foo::getMyField);    
公共接口功能{
O应用(I输入);
}

公共静态您询问如何使用
字段
,并给出了答案。如果出于任何原因,这是一个严格的要求,那么我的答案是没有意义的,但如果不是,这是一个更好的方法,不需要思考

首先,快速提及,它需要是
列出obj
,而不是
T[]obj

现在,对于其余的更改

  • 该方法的类型参数是
    ,我已将其更改为
    F
    代表“字段”
    F
    需要与自身具有可比性,这就是为什么它需要具有可比性的原因
  • 您需要一种从
    T
    获取
    F
    的方法。因此,您给出了一个
    函数,我将在下面给出一个示例
  • compare方法需要返回
    getter.apply(obj1).compareTo(getter.apply(obj2))
  • 这是你的成品

    Foo[] foos = ...
    mySort(foos,
        new Function<Foo, Integer>() {
          @Override public Integer apply(Foo foo) {
            return foo.getMyField();
          }
        });
    
    publicstaticvoidmysort2(列表对象,最终函数getter){
    Collections.sort(obj,newcomparator(){
    公共整数比较(T obj1,T obj2){
    返回getter.apply(obj1.compareTo(getter.apply(obj2));
    }
    });
    }
    
    下面是一个调用它按长度对字符串排序的示例

    public static <T, F extends Comparable<F>> void mySort2(List<T> obj, final Function<T, F> getter) {
        Collections.sort(obj, new Comparator<T>() {
            public int compare(T obj1, T obj2) {
                return getter.apply(obj1).compareTo(getter.apply(obj2));
            }
        });
    }
    
    List List=getSomeListOfStringsSomehow();
    mySort2(列表,(字符串s)->g.length());
    
    谢谢!经过一些尝试和错误,我想我得到了它的工作,因为我问的问题。谢谢!只要我不需要反射地查找字段,我肯定会使用这种类型的设置。不幸的是,我不知道这是否是一个艰难的要求或不是在这个时候(等待别人)。谢谢!这也是一个有趣的解决方案,如果使用字段不是严格的要求,我会考虑。
    Foo[] foos = ...
    mySort(foos,
        new Function<Foo, Integer>() {
          @Override public Integer apply(Foo foo) {
            return foo.getMyField();
          }
        });
    
    public static <T, F extends Comparable<F>> void mySort2(List<T> obj, final Function<T, F> getter) {
        Collections.sort(obj, new Comparator<T>() {
            public int compare(T obj1, T obj2) {
                return getter.apply(obj1).compareTo(getter.apply(obj2));
            }
        });
    }
    
    List<String> list = getSomeListOfStringsSomehow();
    mySort2(list, (String s) -> g.length());