我有一个 Element 类型的 java 集合,想法是按升序对其进行排序。但是,重复的元素应按出现顺序排序。让我用一些代码更好地解释一下:
public class Sorting {
// Element class
public static class Element {
int value;
String position;
}
// Comparator class
public static class ElementComparator implements Comparator<Element> {
@Override
public int compare(Element e1, Element e2) {
if (e1.value != e2.value) return e1.value - e2.value;
return 1; // Elements have same value, so with this condition
// am I guaranteeing that duplicated elements are
// sorted by order of appearance ?
// I have been testing with …Run Code Online (Sandbox Code Playgroud)