4

I have a java collection of type Element and the idea is to sort it in ascending order. However, duplicated elements should be sorted by order of appearance. Let me explain a little bit better with some code:

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 this approach and it has worked fine
                    // but I am not sure how the sort could behave
                    // for other scenarios, eg, multithread environment
                    // I would like to know if this is a safe approach
       }
       @Override
        public String toString() {
            return "Element{" + "number=" + number + ", position=" + position + '}';
        }
    }
    // Main
    public static void main(String[] args) {
        List<Element> elements = new LinkedList<>();
        elements.add(new Element(3, "first 3"));
        elements.add(new Element(2, "first 2"));
        elements.add(new Element(2, "second 2"));
        elements.add(new Element(1, "first 1"));
        elements.add(new Element(1, "second 1"));
        elements.sort(new ElementComparator());
        for (Element e : elements) {
            System.out.println(e);
        }
        // It will print:
        // Element{number=1, position=first 1}
        // Element{number=1, position=second 1}
        // Element{number=2, position=first 2}
        // Element{number=2, position=second 2}
        // Element{number=3, position=first 3}
        // Notice that duplicated elements are sorted by order of appearance
    }
}

In case this approach is not safe, I was considering to create a Wrapper class to do the sort. This wrapper class is something like:

public class ElementWrapperForSorting {
   int value;
   String position;
   int positionOfAppearence;
}

That way, if the value is the same between elements, I can consider the field positionOfAppearence.

4
  • 7
    The term you are looking for is a stable sort. And the default sort algorithm used by Java is a merge sort, which is already stable, so you can just use the standard sort method. Commented Apr 18, 2020 at 19:58
  • @Alex Thank you clarify the term! Commented Apr 18, 2020 at 20:01
  • 2
    The default sorting algorithm used by Java is actually Timsort. Commented Apr 18, 2020 at 20:02
  • 1
    FYI, Wikipedia explains stable sort algorithms and Timsort. Commented Apr 18, 2020 at 20:50

1 Answer 1

5

You don't need the wrapper. List.sort is stable, so equal elements are not reordered. See javadoc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.