[英]Move an element up in the list using Comparator
I have an ArrayList
in Java : 我在Java中有一个ArrayList
:
{"PatMic", "PatientDoc", "Phram", "Patnet", "PatientA"} {“ PatMic”,“ PatientDoc”,“ Phram”,“ Patnet”,“ PatientA”}
All the elements have a number assigned : PatMic = 20, PatientDoc = 30, Phram = 40, Patnet = 50, PatientA = 60. 所有元素均分配有一个数字:PatMic = 20,PatientDoc = 30,Phram = 40,Patnet = 50,PatientA = 60。
And my current Comparator : 和我目前的比较器:
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
final int numbr1 = getElementNumber(); //Returns element's number in a list
final int numbr2 = getElementNumber();
if (numbr1 > numbr2 ) {
return 1;
} else if (numbr1 < numbr2 ) {
return -1;
}
return 0;
}
};
Collections.sort(strings, comparator);
I do not want to change the assigned numbers to each element but would want to move the element PatientA in between PatMic and PatientDoc so the modified list should look like : 我不想更改为每个元素分配的编号,但希望在PatMic和PatientDoc之间移动元素PatientA ,因此修改后的列表应如下所示:
{"PatMic", "PatientA" "PatientDoc", "Phram", "Patnet"} {“ PatMic”,“ PatientA”,“ PatientDoc”,“ Phram”,“ Patnet”}
Could someone please suggest how to achieve this? 有人可以建议如何实现吗? I tried many ways to modify the existing Comparator logic but in vain. 我尝试了许多修改现有比较器逻辑的方法,但徒劳无功。 Thank you. 谢谢。
You are trying to sort based on some inherent value associated with a String. 您正在尝试根据与String关联的某些固有值进行排序。 Therefore, sorting on a String itself is probably not correct. 因此,对字符串本身进行排序可能不正确。 What you probably want to use is either a custom object (implement equals
, hashCode
and the interface Comparable
), or an enum
type. 您可能要使用的是自定义对象(实现equals
, hashCode
和接口Comparable
)或enum
类型。 This will allow you to change the internal state of these objects explicitly, which will manifest itself naturally when using a Comparator
. 这将允许您显式更改这些对象的内部状态,这将在使用Comparator
时自然地表现出来。 For example, using a class: 例如,使用一个类:
class MyClass implements Comparable
{
private String name;
private int value;
//Constructor
public MyClass(String s, int v)
{
name = s;
value = v;
}
//Getters and setters
//Implement comparing method
}
Then you can use these objects in place of your Strings: 然后,您可以使用以下对象代替字符串:
//...
MyClass patMic = new MyClass("PatMic", 20);
// So on..
First, you should give you comparator sufficient knowledge about what it should do. 首先,您应该给比较器足够的知识,以了解它应该做什么。 I mean you should have some data available to comparator that says something like "okay, sort them all by associated number except this one - place it right here ". 我的意思是,您应该为比较器提供一些数据,例如“好吧, 除此数字外 ,均按相关数字对它们进行排序-放在此处 ”。 " Right here " could be anything that points exact position, I gonna choose " before that element ". “ 在这里 ”可以是指向确切位置的任何内容,我将选择“ 在该元素之前 ”。
So here we go 所以我们开始
public void sortWithException(List<String> data, final Map<String, Integer> numbers, final String element, final String next) {
Collections.sort(data, new Comparator<String>() {
@Override
public int compare(String first, String second) {
if (first.equals(element) || second.equals(element)) { //the exception
Integer nextNumber = numbers.get(next);
Integer firstNumber = numbers.get(first);
Integer secondNumber = numbers.get(second);
if (first.equals(element)) {
if (next == null) // placing the exception after ANY element
return 1;
return secondNumber >= nextNumber ? -1 : 1; //placing the element before next and after all next's predecessors
} else { // second.equals(element)
if (next == null)
return -1;
return firstNumber >= nextNumber ? 1 : -1;
}
} else { //normal sort
return numbers.get(first) - numbers.get(second);
}
}
});
}
and call it like sortWithException(data, numbers, "PatientA", "PatientDoc")
并像sortWithException(data, numbers, "PatientA", "PatientDoc")
一样调用它
Note that i used Map for associated numbers, you should probably use your own method to get those numbers. 请注意,我使用Map来关联数字,您可能应该使用自己的方法来获取这些数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.