简体   繁体   中英

Sorting values in ArrayList in specific order

Let me explain the scenario first:

List<Row> rowValues = new ArrayList<>();

// After adding values into list
At location 0 = [Johnson, 10000]
At location 1 = [Adam, 12000]
At location 2 = [Mike, 11000]
At location 3 = [Johnson, 17000]
At location 4 = [Tony, 10000]

I want to sort elements of column1 in ascending order and elements of column2 in descending order. Like:

At location 0 = [Adam, 12000]
At location 1 = [Johnson, 17000]
At location 2 = [Johnson, 10000]
At location 3 = [Mike, 11000]
At location 4 = [Tony, 10000]

I am not sure whether this can achieve this by using:

Collections.sort(rowValues); //or
Collections.sort(rowValues, Collections.reverseOrder());

Row class, if that makes any sense:

final class Row extends ArrayList<Object> implements Comparable<Row> {

    private int index;
    private Order order;


    Row(int initialCapacity) {
        super(initialCapacity);
        this.index = -1; //-1 indicates that the index has not been set
    }

    Object getSortingValue() {
        if (index == -1) {
            throw new IllegalStateException("Sorting column is unknown");
        } else if (isEmpty()) {
            throw new IllegalStateException("Row is empty");
        }
        return get(index);
    }

   void setSortingColumn(int index) throws IllegalArgumentException {
        if (index < 0) {
            throw new IllegalArgumentException("Invalid sorting index: " + index);
        }
        this.index = index;
    }

    Order getOrder() {
        return order;
    }

    void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public int compareTo(Row row) {
        if (row == null) {
            throw new NullPointerException();
        }
        Object sortValue = getSortingValue();
        if (sortValue instanceof Comparable) {
            return ((Comparable) sortValue).compareTo(row.getSortingValue());
        } else {
            throw new IllegalArgumentException(sortValue + " not type of Comparable");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Row) {
            Row row = (Row) obj;
            return getSortingValue().equals(row.getSortingValue());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return getSortingValue().hashCode();
    }
}

是的,您可以使用Collections.sort()对其进行排序,但是您需要定义自己的Comparator ,在其中,当第一行较大或相等时,第二行较大时定义一个对象大于其他对象。 。

You need to account for 2 values(for both columns) in compareTo() method, Assuming that corresponding field in the Row object are named name and point -

 public int compareTo(Row row) {
        if(!name.equals(row.getName()){
        return name.compareTo(row.getName());
        }
         return  -1 * point.compareTo(row.getPoint());
    }

I think you want to sort by name asc, and if the name is equal to another, then sort by value desc. If so, then

    List<Row> rowValues = new ArrayList<Row>();

    // After adding values into list
    rowValues.add(new Row("Johnson", 10000));
    rowValues.add(new Row("Adam", 12000));
    rowValues.add(new Row("Mike", 11000));
    rowValues.add(new Row("Johnson", 17000));
    rowValues.add(new Row("Tony", 10000));

    Collections.sort(rowValues, new RowComparator());

    System.out.println(rowValues);

and

public class Row {
    private String name;
    private Integer val;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getVal() {
        return val;
    }
    public void setVal(Integer val) {
        this.val = val;
    }
    public Row(String name, Integer val) {
        super();
        this.name = name;
        this.val = val;
    }
    @Override
    public String toString() {
        return "Row [name=" + name + ", val=" + val + "]";
    }

}

and

import java.util.Comparator;

public class RowComparator implements Comparator<Row> {

    public int compare(Row o1, Row o2) {
        if (o1.getName().equals(o2.getName())) {
            return -1 * o1.getVal().compareTo(o2.getVal());
        } else {
            return o1.getName().compareTo(o2.getName());
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM