java按可指定属性排序对象列表 [英] java sort list of objects by specifiable attribute

查看:149
本文介绍了java按可指定属性排序对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这些对象的指定属性对对象列表进行排序,我想选择应该用于排序的属性。示例:

I want to sort a List of objects by a specified attribute of those objects and I want to choose which attribute should be used for sorting. Example:

class Car{
  private String name;
  private String colour;
  public enum sortBy {NAME, COLOUR};

  public String name(){
    return name;
  }

  public String colour(){
    return colour;
  }

  public static Car[] getSortedArray(Car[] carArray, sortBy sortType){
    HashMap<Object, Car> carMap = new HashMap<Object, Car>();
    Object[] sortArray = new Object[carArray.length];
    Object value = null;
    for(int i = 0; i < carArray.length; i++){
      if(sortType == sortBy.NAME){
        value = carArray[i].name();
      }else if(sortType == sortBy.COLOUR){
        value = carArray[i].colour();
      }
      carMap.put(value, carArray[i]);
      sortArray[i] = value;
    }  
    Arrays.sort(sortArray);
    Car[] sortedArray = new Car[sortArray.length];
    for(int i = 0; i < sortArray.length; i++){
      sortedArray[i] = carMap.get(sortArray[i]);
    }
    return sortedArray;
  }
}

//external:
Car[] cars = getSomeCars();
Car[] nameSortedCars = Car.getSortedArray(cars, Car.sortBy.NAME);
Car[] colourSortedCars = Car.getSortedArray(cars, Car.sortBy.COLOUR);

这个想法很简单:

我把我要排序的所有值都排序通过进入一个数组,我创建一个地图,将这些值映射回他们的对象。在对这个数组进行排序之后,我将映射到这些值的对象放在一个新的数组中,然后按这些值排序。这些值只是使用Object类型创建的,所以我可以按多种类型排序(不仅仅是示例中的字符串)。

The idea is simple:
I put all values that i want to sort by into an array, and i create a map that maps these values back to their objects. After I sorted this array I take the objects mapped to these values and put them in the same order into a new array which is then sorted by these values. The values are just created with type Object so I can sort by multiple types (not just Strings as in the example).

除非你有两个对象,否则这个工作正常相同的属性值,然后只有一个对象将在返回的数组中,但两次。

有没有更好的方法来实现这种排序?

This works fine unless you have two objects with the same attribute value, then only one object will be in the returned array, but two times.
Is there a better way to achieve this sorting?

推荐答案

使用自定义比较器要简单得多:

It would be much simpler to use custom comparators:

名称排序

Arrays.sort(carArray, Comparator.comparing(Car::name));

颜色排序

Arrays.sort(carArray, Comparator.comparing(Car::colour));

所以你可以修改 getSortedArray()

public static Car[] getSortedArray(Car[] carArray, Comparator<Car> comparator) {
    Car[] sorted = carArray.clone()
    Arrays.sort(sorted, comparator);
    return sorted;
}

并将其称为:

Car[] sorted = getSortedArray(carArray, Comparator.comparing(Car::name));

修改:

如果您使用的语言版本不支持这些功能,则可以通过显式创建实现 Comparator 界面的嵌套类来创建比较器。

If you use a language version that does not support these features, you can create the comparators by explicitly creating a nested class that implements the Comparator interface.

例如,这是一个比较 Car 实例的单件比较器 by name

This, for example, is a singleton Comparator that compares Car instances by name:

static enum ByName implements Comparator<Car> {
    INSTANCE;

    @Override
    public int compare(Car c1, Car c2) {
        return c1.name().compareTo(c2.name());
    }
}

然后致电:

Car[] sorted = getSortedArray(carArray, ByName.INSTANCE);

这篇关于java按可指定属性排序对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆