package org.plumelib.util;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/** A comparator that orders values based on the lexicographic ordering of their toString(). */
public class ToStringComparator implements Comparator<Object> {
  /** The unique instance (this class is a singleton). */
  public static ToStringComparator instance = new ToStringComparator();

  /** Creates a ToStringComparator. */
  private ToStringComparator() {}

  @Override
  public int compare(Object o1, Object o2) {
    return Objects.toString(o1).compareTo(Objects.toString(o2));
  }

  /**
   * Returns a copy of the input, sorted according to the elements' {@code toString()}.
   *
   * @param <T> the type of the elements
   * @param in a set of elements
   * @return the elements, sorted according to {@code toString()}
   */
  public static <T> List<T> sorted(Iterable<T> in) {
    List<T> result = new ArrayList<T>();
    for (T object : in) {
      result.add(object);
    }
    result.sort(instance);
    return result;
  }
}

@@ -131,16 +131,19 @@
 ts;%0A%0A/**
+%0A *
  A compa
@@ -223,19 +223,328 @@
 tring().
+%0A
  *
-/
+%0A * %3Cp%3EIt handles null values, sorting them according to their printed representation %22null%22.%0A */%0A// Once https://github.com/typetools/checker-framework/issues/1970 is fixed, Comparator's type%0A// argument should be marked as @Covariant and this should be declared as %22extends%0A// Comparator%3C@Nullable Object%3E%22.
 %0Apublic 

Add comments