仅根据字段名称对 类 的数组进行排序

Sorting an array of classes based only on field name

我有一个应用程序,用户向我提供字段名称,例如 namecostInCents,我必须按该字段进行排序。我有办法保证字段名称是正确的。这个应用程序导致我根本无法使我的 class Comparable 和实现特定的 compareTo() 的复杂化,因为使用 compareTo() 的自定义实现我需要知道哪些字段/方法在实施时使用。

所以为了实现这个目标,我正在尝试使用反射来将字段与其访问器匹配。这是我想做的 MWE。

Class Product 是一个简单的 POJO class 我想成对比较其实例:

public class Product
{

    final String name;
    final Integer quantity;
    final Long costInCents;

    public Product(final String name, final Integer quantity, final Long costInCents)
    {
        this.name = name;
        this.quantity = quantity;
        this.costInCents = costInCents;
    }

    public String getName()
    {
        return name;
    }
    public Integer getQuantity()
    {
        return quantity;
    }
    public Long getCostInCents()
    {
        return costInCents;
    }
}

还有我的Mainclass,目前不完整:

public class Main {

    public static void main(String[] args) {
        final Product[] productArray =
                {
                    new Product("Clorox wipes", 50, 700L),
                    new Product("Desk chair", 10, 12000L),
                    new Product("TV", 5, 30000L),
                    new Product("Bookcase", 5, 12000L),
                    new Product("Water bottle", 20, 700L),
                };

        // The following void methods are supposed to sort in-place with something like Arrays.sort() or Collections.sort(),
        // but I am also open to solutions involving stuff like Stream::sorted() or similar ones, which return a sorted array.
        sortByField(productArray, "costInCents");
        sortByField(productArray, "name");
    }

    private void sortByField(final Product[] productArray, final String sorterFieldName)
    {
        final Field sorterField = getSorterField(sorterFieldName, LiteProduct.class); // Gets the Field somehow
        final Method sorterAccessor = getSorterAccessor(sorterField, LiteProduct.class);    // Given the Field, this is easy
        Arrays.sort((Product p1, Product p2)->((Comparable<?>)sorterAccessor.invoke(p1)).compareTo(sorterAccessor.invoke(p2)) > 0); // Capture of ? instead of Object
    }
}

不幸的是,Arrays.sort() 行导致编译时错误消息 Capture of ? instead of Object。我曾尝试将第二个参数转换为 Comparable<?>Comparable<? super sorterField.getType() 等,但没有成功。想法?

你可以试试这个:

public class Test{
  public static void main(String arg[]){
  final Product[] productArray =
            {
                new Product("Clorox wipes", 50, 700L),
                new Product("Desk chair", 10, 12000L),
                new Product("TV", 5, 30000L),
                new Product("Bookcase", 5, 12000L),
                new Product("Water bottle", 20, 700L),
            };
            
      Arrays.sort(productArray,(p1, p2) -> p1.getName().compareTo(p2.getName()));
 
     for(Product p: productArray){
        System.out.println(p.getName());
     }
    }
   }

在这种情况下,Comparator 是一个函数式接口,所以我使用了 lambad 表达式。但你也可以这样做

您可以为每个字段编写一个比较器,并通过映射按名称使用它:

public class Product
{
    private final static Map<String,Comparator<Product>> COMPARATORS;

    static {
        COMPARATORS = new HashMap<>();
        COMPARATORS.put("name", new NameComparator());
        COMPARATORS.put("costInCents", new CostInCentsComparator());
    }

    final String name;
    final Integer quantity;
    final Long costInCents;

    public Product(final String name, final Integer quantity, final Long costInCents)
    {
        this.name = name;
        this.quantity = quantity;
        this.costInCents = costInCents;
    }

    public String getName()
    {
        return name;
    }
    public Integer getQuantity()
    {
        return quantity;
    }
    public Long getCostInCents()
    {
        return costInCents;
    }

    static class NameComparator implements Comparator<Product> {

        @Override
        public int compare(Product o1, Product o2) {
            return o1.getName().compareTo(o2.getName());
        }

    }

    static class CostInCentsComparator implements Comparator<Product> {

        @Override
        public int compare(Product o1, Product o2) {
            return o1.getCostInCents().compareTo(o2.getCostInCents());
        }

    }

    static Comparator<Product> getComparator(String name) {
        return COMPARATORS.get(name);
    }

}

并在主要 class

中使用它
public class Main {

    public static void main(String[] args) {
        final Product[] productArray =
                {
                    new Product("Clorox wipes", 50, 700L),
                    new Product("Desk chair", 10, 12000L),
                    new Product("TV", 5, 30000L),
                    new Product("Bookcase", 5, 12000L),
                    new Product("Water bottle", 20, 700L),
                };

        // The following void methods are supposed to sort in-place with something like Arrays.sort() or Collections.sort(),
        // but I am also open to solutions involving stuff like Stream::sorted() or similar ones, which return a sorted array.
        sortByField(productArray, "costInCents");
        sortByField(productArray, "name");
    }

    private static void sortByField(final Product[] productArray, final String sorterFieldName)
    {
        Arrays.sort(productArray, Product.getComparator(sorterFieldName));
    }
}

你可能需要做一些小的改变,比如让它成为 null safe 之类的

可能是最好的方法 - 使用排序策略。无需反射,兼容更复杂的排序逻辑:

Map<String, Comparator<Product>> sortingStrategies = new HashMap<>(){
    {
        put("costInCents", Comparator.comparingLong(p->p.costInCents));
        put("quantity", Comparator.comparingLong(p->p.quantity));
        put("name", Comparator.comparing(p->p.name));
    }
};

private void sortByField(final Product[] productArray, final String sorterFieldName)
{
    Arrays.sort(productArray, sortingStrategies.get(sorterFieldName));
}