1

I tried to create a generic next_permutation() function which would do the same thing as the C++ next_permutation() function. The compiler says:

bad operand types for binary operator '>' and '<',

at the following lines:

  • if (data.get(last) < data.get(last + 1))
  • if (if data.get(i) > data.get(last))

How do I resolve this?

public static <T> boolean findNextPermutation(List<T> data) 
{ 


    if (data.size() <= 1) 
        return false; 

    int last = data.size() - 2; 

    while (last >= 0) { 
        if (data.get(last) < data.get(last + 1)) { 
            break; 
        } 
        last--; 
    } 

    if (last < 0) 
        return false; 

    int nextGreater = data.size() - 1; 

    for (int i = data.size() - 1; i > last; i--) { 
        if (data.get(i) > data.get(last)) { 
            nextGreater = i; 
            break; 
        } 
    } 

    data = swap(data, nextGreater, last); 

    data = reverse(data, last + 1, data.size() - 1); 

    return true; 
} 
Magnilex
  • 11,584
  • 9
  • 62
  • 84
neo360
  • 49
  • 5

2 Answers2

2

T can be anything. It might be of type Integer or of type String. An Integer can be unboxed to an int and compared with < or >, but a String cannot.

You need to narrow down what T can be. One way would be to force T to implement Comparable. You can then make use of the compareTo() method:

public static <T extends Comparable<T>> boolean findNextPermutation(List<T> data) {

  if (data.size() <= 1)
    return false;

  int last = data.size() - 2;

  while (last >= 0) {
    if (data.get(last).compareTo(data.get(last + 1)) < 0) {
      break;
    }
    last--;
  }

  if (last < 0)
    return false;

  int nextGreater = data.size() - 1;

  for (int i = data.size() - 1; i > last; i--) {
    if (data.get(i).compareTo(data.get(last)) > 0) {
      nextGreater = i;
      break;
    }
  }

  data = swap(data, nextGreater, last);

  data = reverse(data, last + 1, data.size() - 1);

  return true;
}
Magnilex
  • 11,584
  • 9
  • 62
  • 84
1

If T does not or can not implement Comparable as proposed by this answer you can supply a Comparator<T> and use it:

public static <T> boolean findNextPermutation(List<T> data, Comparator<T> comperator) {

      if (data.size() <= 1)  return false;

      int last = data.size() - 2;

      while (last >= 0) {
        if (comperator.compare(data.get(last),data.get(last + 1)) < 0 ){
          break;
        }
        last--;
      }

      if (last < 0) return false;

      int nextGreater = data.size() - 1;

      for (int i = data.size() - 1; i > last; i--) {
        if (comperator.compare(data.get(i), data.get(last)) > 0) {
          nextGreater = i;
          break;
        }
      }

      data = swap(data, nextGreater, last);
      data = reverse(data, last + 1, data.size() - 1);

      return true;
}

Side note: you can make a null friendly cooperator. For example:
Comparator<T> nullSafeComperator = Comparator.nullsFirst(comperator);

c0der
  • 18,467
  • 6
  • 33
  • 65