如何通过自定义比较器对包含扩展类的列表进行排序?

问题描述 投票:0回答:1

我似乎找不到通过“rent()”和“profit()”对列表列表进行排序的方法。我怎样才能实现这一目标?

似乎无法找到任何有用的东西。

家长班:

public abstract class Zone
{
    int area; //zonos plotas kv metrais
    float price; //kaina uz kv metra
    int jobPlaces; //darbo vietu skaicius
    abstract float rent();
    abstract float profit();
    abstract float expenses();
    abstract float totalProfit();
}

我有3个类(住宅,商业,工业)扩展这个类并覆盖其抽象方法。

我定义列表:

static ArrayList<Zone> coll1 = new ArrayList<Zone>();
static ArrayList<Zone> coll2 = new ArrayList<Zone>();

static ArrayList<ArrayList<Zone>> superCollection = new ArrayList<ArrayList<Zone>>();

然后我随机(例如3个住宅区,5个商业区,1个工业区)填写coll1和coll2并将它们添加到superCollection。

我如何通过“rent()”打印它来排序superCollection,然后按升序或降序对“profit()”进行排序?

编辑:

Collections.sort(superCollection, new Comparator<ArrayList<Zone>>()
{
    @Override
    public int compare(ArrayList<Zone> arg0, ArrayList<Zone> arg1)
    {
        return arg0.get(0).profit().compareTo(arg1.get(0).profit());
    }
});

尝试运行这个,但它仍然没有按利润排序:

zone                 rent           profit                          
Residential zone:   11578.3         5534.4             
Residential zone:    1963.7         2935.1              
Residential zone:    4029.5         4987.2             
Residential zone:   13399.6        11453.7             
Residential zone:    2763.7         7212.9              
Residential zone:    3961.3         3384.8                  
Commercial zone:    29041.3        59291.3             
Commercial zone:    10483.6        42842.5             
Industrial zone:    48332.3       939667.0          
Industrial zone:    31563.8      1074516.2            
Residential zone:    8347.1         3587.1                 
Commercial zone:    26177.9        47750.9            
Industrial zone:    33917.8      1005413.1            
Industrial zone:    25704.2      1251655.3            
Industrial zone:    30268.5      1131300.0             
Industrial zone:    42225.1       588861.8                            
Industrial zone:    32779.2      1220447.5          
Industrial zone:    19686.2       863131.5 
java sorting compare comparator comparable
1个回答
0
投票

由于您需要按两种不同方式打印列表,因此需要两个比较器。将超级集合中的列表组合到一个列表中,使用第一个比较器对其进行排序,打印,使用第二个比较器对其进行排序,然后再次打印。

private Comparator<Zone> rentComparator = new Comparator<Zone>() {

        @Override
        public int compare(Zone o1, Zone o2) {
            Float o1Rent = o1.rent();
            Float o2Rent = o2.rent();

            return o1Rent.compareTo(o2Rent);
        }
    };

private Comparator<Zone> profitComparator = new Comparator<Zone>() {

        @Override
        public int compare(Zone o1, Zone o2) {
            Float o1Rent = o1.profit();
            Float o2Rent = o2.profit();

            return o1Rent.compareTo(o2Rent);
        }
    };

public void printSortedLists(List<List<Zone>> superCollection) {

    List<Zone> combinedList = new ArrayList<>();
    for (List<Zone> list : superCollection) {
        combinedList.addAll(list);
    }
    Collections.sort(combinedList, rentComparator);

    //TODO now print the rent sorted list here

    Collections.sort(combinedList, profitComparator);

    //TODO now print the profit sorted list here
}
© www.soinside.com 2019 - 2024. All rights reserved.