小编4ch*_*nos的帖子

Java 对具有重复元素的集合进行排序。如果重复,按出现顺序排序

我有一个 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)

java sorting

4
推荐指数
1
解决办法
289
查看次数

标签 统计

java ×1

sorting ×1