fork download
  1. import java.util.*;
  2. import java.util.function.*;
  3. import java.time.*;
  4. class Ideone {
  5. private static <X, Y> List<Y> mapi(BiFunction<Integer, X, Y> f, List<X> xs) {
  6. List<Y> ys = new ArrayList<>();
  7. for (int i = 0; i < xs.size(); i++)
  8. ys.add(f.apply(Integer.valueOf(i), xs.get(i)));
  9. return ys;
  10. }
  11. private static <X> List<X> sort(Comparator<X> c, List<X> xs) {
  12. List<X> ys = new ArrayList<>(xs);
  13. ys.sort(c);
  14. return ys;
  15. }
  16. private static <X, Y> List<Y> map(Function<X, Y> f, List<X> xs) {
  17. List<Y> ys = new ArrayList<>();
  18. for (X x : xs) ys.add(f.apply(x));
  19. return ys;
  20. }
  21. private static <E extends Comparable<E>> List<Integer> sorti(List<E> cs) {
  22. /*
  23.   record IndexedValue<T extends Comparable<? super T>>(Integer index, T value) implements Comparable<IndexedValue<T>> {
  24.   public int compareTo(IndexedValue<T> other) {
  25.   return value.compareTo(other.value);
  26.   }
  27.   }
  28.   */
  29. class IndexedValue<T extends Comparable<? super T>> implements Comparable<IndexedValue<T>> {
  30. private Integer index;
  31. private T value;
  32. public Integer index() {return index;}
  33. public IndexedValue(Integer index, T value) {this.index = index;this.value = value;}
  34. public int compareTo(IndexedValue<T> other) {return value.compareTo(other.value);}
  35. }
  36. return map(IndexedValue<E>::index, sort(IndexedValue<E>::compareTo, mapi(IndexedValue<E>::new, cs)));
  37. }
  38. private static <E extends Comparable<E>> List<Integer> f(List<E> cs) {
  39. return sorti(sorti(cs));
  40. }
  41. private static <E extends Comparable<E>> void g(List<E> cs) {
  42. System.out.println(f(cs));
  43. }
  44. public static void main(String[] args) {
  45. g(Arrays.asList(1,100,10,10000,1000));
  46. g(Arrays.asList(3,1,4,1,5,9,2));
  47. g(Arrays.asList(0,1,0,1,0,1,0,1));
  48. g(Arrays.asList("A","C","B","E","D"));
  49. g(Arrays.asList(Month.MARCH, Month.JANUARY, Month.APRIL, Month.JANUARY, Month.MAY, Month.SEPTEMBER, Month.FEBRUARY));
  50. }
  51. }
  52.  
Success #stdin #stdout 0.09s 55756KB
stdin
Standard input is empty
stdout
[0, 2, 1, 4, 3]
[3, 0, 4, 1, 5, 6, 2]
[0, 4, 1, 5, 2, 6, 3, 7]
[0, 2, 1, 4, 3]
[3, 0, 4, 1, 5, 6, 2]