I know my solution is a little convoluted, but I'm failing the requirement of words in ascending order, even though this solution works for all the cases I've tried and I am calling the isGreater method. Any nuggets of wisdom?
package com.codegym.task.task09.task0930;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
while (true) {
String s = reader.readLine();
if (s.isEmpty()) break;
list.add(s);
}
String[] array = list.toArray(new String[list.size()]);
sort(array);
for (String x : array) {
System.out.println(x);
}
}
public static void sort(String[] array) {
for (int i = 0; i < array.length; i++) {
int d = 0; // increment for while loop to move pointer until matching types are found
for (int j = 1; j < array.length - i; j++) {
// all possible permutations of a bubble sort
System.out.println("All permutations:" + (j - 1) + " : " + j) ;
// while we are here, stay until we find same types down the line
// if same type, while loop will not run
while ((isNumber(array[j - 1]) != isNumber(array[j + d]))) {
if (j < array.length - d - 1) // d can't get any bigger so that it goes outside length of array we are checking
d++;
else
break;
System.out.println (" " + (j - 1) + " : " + (j + d));
}
if((isNumber(array[j-1]) && isNumber(array[j+d]) && (Integer.parseInt(array[j-1]) < Integer.parseInt(array[j+d]))) ||
(!isNumber(array[j-1]) && !isNumber(array[j+d]) && isGreaterThan(array[j-1], array[j+d]))) {
String temp = array[j-1];
array[j-1] = array[j+d];
array[j+d] = temp;
}
d = 0; // reset for next iteration of for loop
}
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(String a, String b) {
return a.compareTo(b) > 0;
}
// Is the passed string a number?
public static boolean isNumber(String s) {
if (s.length() == 0) return false;
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((i != 0 && c == '-') // The string contains a hyphen
|| (!Character.isDigit(c) && c != '-') // or is not a number and doesn't start with a hyphen
|| (i == 0 && c == '-' && chars.length == 1)) // or is a single hyphen
{
return false;
}
}
return true;
}
}
// Task about algorithms
// Task: The user enters a list of words (and numbers) from the keyboard. The words are displayed in ascending order, the numbers in descending order.
//
// Example input:
// Cherry
// 1
// Bob
// 3
// Apple
// 22
// 0
// Watermelon
//
// Example output:
// Apple
// 22
// Bob
// 3
// Cherry
// 1
// 0
// Watermelon
//
//
// Requirements:
// 1. The program must read data from the keyboard.
// 2. The program should display data on the screen.
// 3. The displayed words should be sorted in ascending order (using the provided isGreaterThan method).
// 4. The displayed numbers must be sorted in descending order.
// 5. The main method should use the sort method.
// 6. The sort() method should call the isGreaterThan() method.
// 7. The sort() method should call the isNumber() method.
// j + displacement = array.length - 1
//