我有一个Duck对象的集合,我想用多个键对它们进行排序.
class Duck {
DuckAge age; //implements Comparable
DuckWeight weight; //implements Comparable
String name;
}
List<Duck> ducks = Pond.getDucks();
Run Code Online (Sandbox Code Playgroud)
例如.我想主要根据他们的重量来排序,其次是他们的年龄.如果两只鸭子具有完全相同的重量和完全相同的年龄,那么让我们使用它们的名称作为三级键来区分它们.我可能会这样做:
Collections.sort(ducks, new Comparator<Duck>(){
@Override
public int compare(Duck d1, Duck d2){
int weightCmp = d1.weight.compareTo(d2.weight);
if (weightCmp != 0) {
return weightCmp;
}
int ageCmp = d1.age.compareTo(d2.age);
if (ageCmp != 0) {
return ageCmp;
}
return d1.name.compareTo(d2.name);
}
});
Run Code Online (Sandbox Code Playgroud)
我经常这样做,但这个解决方案闻不到.它不能很好地扩展,并且很容易搞砸.当然必须有一种更好的方法来使用多个键对Ducks进行排序!有人知道更好的解决方案吗?
EDIT删除了不必要的else
分支
我有一个实现静态方法的类学生
public static Comparator<Student> getCompByName()
Run Code Online (Sandbox Code Playgroud)
返回Student的新比较器对象,通过属性"name"比较2个Students对象.
我现在需要使用我的函数getCompByName()通过'name'对学生ArrayList进行排序来测试它.
这是我的Student课程中的Comparator方法.
public static Comparator<Student> getCompByName()
{
Comparator comp = new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2)
{
return s1.name.compareTo(s2.name);
}
};
return comp;
}
Run Code Online (Sandbox Code Playgroud)
而主要我需要测试的地方
public static void main(String[] args)
{
// TODO code application logic here
//--------Student Class Test-------------------------------------------
ArrayList<Student> students = new ArrayList();
Student s1 = new Student("Mike");
Student s2 = new Student("Hector");
Student s3 = new Student("Reggie");
Student s4 = new Student("zark");
students.add(s1);
students.add(s2);
students.add(s3);
students.add(S4);
//Use getCompByName() …
Run Code Online (Sandbox Code Playgroud) 我有一个对象列表,我想使用不同的属性进行排序.
@Override
public int compareTo(Object obj)
{
Field tab = (Field) obj;
int objx = Integer.parseInt(tab.getX());
// int objy = Integer.parseInt(tab.getY());
int classX = Integer.parseInt(this.X);
if (classX == objx)
return 0;
else if (classX > objx)
return 1;
else
return -1;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我所拥有的:
Collections.sort(list1); // But using property1 to sort
Collections.sort(list1); // But using property2 to sort
Run Code Online (Sandbox Code Playgroud)
所以,在第一种情况下,我能够使用property1排序,但如何使用property2排序?我正在尝试使用不同的参数进行排序,但compareTo()
只接受一个参数.
给一个班级.
public class Entity {
private Long id;
private String prodName;
private BigDecimal price;
// Constructors + getters + setters + hashcode() + equals() + toString().
}
Run Code Online (Sandbox Code Playgroud)
构建一个列表Entity
.
List<Entity> list = new ArrayList<>();
Entity entity = new Entity();
entity.setId(1L);
entity.setProdName("A");
entity.setPrice(new BigDecimal(10));
list.add(entity);
entity = new Entity();
entity.setId(2L);
entity.setProdName("B");
entity.setPrice(new BigDecimal(20));
list.add(entity);
entity = new Entity();
entity.setId(3L);
entity.setProdName("C");
entity.setPrice(new BigDecimal(30));
list.add(entity);
entity = new Entity();
entity.setId(4L);
entity.setProdName("D");
entity.setPrice(new BigDecimal(40));
list.add(entity);
entity = new Entity();
entity.setId(5L);
entity.setProdName("E");
entity.setPrice(new BigDecimal(50)); …
Run Code Online (Sandbox Code Playgroud) 让我先解释一下这个场景:
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]
Run Code Online (Sandbox Code Playgroud)
我想column1
按升序排列元素,column2
按降序排列元素.喜欢:
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]
Run Code Online (Sandbox Code Playgroud)
我不确定这是否可以通过以下方式实现:
Collections.sort(rowValues); //or …
Run Code Online (Sandbox Code Playgroud) java ×5
sorting ×5
comparator ×4
collections ×2
arraylist ×1
comparable ×1
java-8 ×1
java-stream ×1