[Solved] Condition for loops in java [closed]


Sounds like you are asking how to take a Predicate (BiPredicate<String, String> to be precise) as a parameter.

  public void bubbleSort(String[] part, BiPredicate<String, String> predicate) {
    while(predicate.test(part[i], part[i-1])) {
      // Do bubblesort stuff
    }
  }

  public void bubbleSort(String[] part) {
    if (isText) {
      bubbleSort(part, (l, r) -> l.compareToIgnoreCase(r) < 0);
    } else {
      bubbleSort(part, (l, r) -> Integer.parseInt(l) < Integer.parseInt(r));
    }
  }

That said there are better ways to sort arrays! 🙂

Arrays.sort(part, String::compareToIgnoreCase); // Sort by text
Arrays.sort(part, Comparator.comparingInt(Integer::parseInt)); // Sort by number

solved Condition for loops in java [closed]