有Person
类的对象,如:M1,M2,M3,M4,M5,W1,W2,W3,W4,C1,C2。 M:Man,W:Woman,C:Child
它们存储在下面的数组中:
Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5};
现在必须根据为每种类型的对象设置的优先级重新排列数组。优先权在Enum中给出:
enum Priority{
One,
Two,
Three;
}
同时确保订单保持不变,例如:M1应该在M2之前,M2应该在M3之前,依此类推......
输入: Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5};
人的优先权:优先权。二
女性优先:Priority.One
儿童优先:优先。三
预期输出: Person[] arr = {W1,W2,W3,W4,M1,M2,M3,M4,M5,C1,C2};
输出错误: Person[] arr = {W1,W3,W2,W4,M1,M5,M4,M3,M2,C2,C1};
后者是错误的,因为订单也必须保持不变。
答案 0 :(得分:2)
尝试以下
final List<Person> persons = new ArrayList<>();
IntStream.rangeClosed(1, 5).mapToObj(i -> new Person("M" + i, Priority.TWO)).forEach(persons::add);
IntStream.rangeClosed(1, 4).mapToObj(i -> new Person("W" + i, Priority.ONE)).forEach(persons::add);
IntStream.rangeClosed(1, 2).mapToObj(i -> new Person("C" + i, Priority.THREE)).forEach(persons::add);
persons.add(new Person("M11", Priority.TWO)); // test to sort by number
List<Person> sorted = persons.stream()
.sorted(Comparator.comparing(Person::getPriority).thenComparingInt(p -> Integer.parseInt(p.getName().substring(1))))
.collect(Collectors.toList());
System.out.println("Before sort " + persons.stream().map(Person::getName).collect(Collectors.toList()));
System.out.println("After sort " + sorted.stream().map(Person::getName).collect(Collectors.toList()));
输出中
Before sort [M1, M2, M3, M4, M5, W1, W2, W3, W4, C1, C2, M11]
After sort [W1, W2, W3, W4, M1, M2, M3, M4, M5, M11, C1, C2]
请注意:枚举中的值是有序的,上面的代码取决于枚举类中值的顺序
修改-1 强>
Compartor
Comparator<Person> comp = new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
int co = p1.getPriority().compareTo(p2.getPriority());
if (co == 0)
return Integer.parseInt(p1.getName().substring(1)) - Integer.parseInt(p2.getName().substring(1));
return co;
}
};
答案 1 :(得分:1)
我相信你必须为Person Class创建自己的比较器,这样你的Person对象可以使用Collections.sort()方法在集合中进行排序。
这是一个如何为类创建自己的比较器的链接 http://www.tutorialspoint.com/java/java_using_comparator.htm
答案 2 :(得分:1)
以下是使用Comparator界面对自定义对象进行排序的解决方案。
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Person {
private int priority;
private String objName;
Person(String name,int value){
this.objName = name;
this.priority = value;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getObjName() {
return objName;
}
public void setObjName(String objName) {
this.objName = objName;
}
public static void main(String[] args) {
Person M1 = new Person("M1",Priority.valueOf("ONE").getValue());
Person M2 = new Person("M2",Priority.valueOf("ONE").getValue());
Person M3 = new Person("M3",Priority.valueOf("ONE").getValue());
Person M4 = new Person("M4",Priority.valueOf("ONE").getValue());
Person M5 = new Person("M5",Priority.valueOf("ONE").getValue());
Person W1 = new Person("W1",Priority.valueOf("THREE").getValue());
Person W2 = new Person("W2",Priority.valueOf("THREE").getValue());
Person W3 = new Person("W3",Priority.valueOf("THREE").getValue());
Person W4 = new Person("W4",Priority.valueOf("THREE").getValue());
Person C1 = new Person("C1",Priority.valueOf("TWO").getValue());
Person C2 = new Person("C2",Priority.valueOf("TWO").getValue());
Person[] arr = {M1,M3,C1,W1,W3,M2,M4,W2,C2,W4,M5};
List<Person> list = Arrays.asList(arr);
System.out.println("Before sort...");
for(Person p : list){
System.out.println(p.getObjName());
}
Collections.sort(list, new PersonComparator<Person>());
System.out.println("After sort...");
for(Person p : list){
System.out.println(p.getObjName());
}
}
}
enum Priority{
ONE(1),TWO(2),THREE(3);
private int value;
public int getValue() {
return value;
}
Priority(int v){
this.value = v;
}
}
class PersonComparator<T> implements Comparator<Person> {
public int compare(Person p1, Person p2) {
//Sorting based on priority
int v1 = p1.getPriority();
int v2 = p2.getPriority();
;
if (v1 - v2 == 0) {
//Sorting based on object name
int i1 = Integer.parseInt(p1.getObjName().substring(1, 2));
int i2 = Integer.parseInt(p2.getObjName().substring(1, 2));
return i1 - i2;
}
return v1 - v2;
}
}