Alright, I'm confused.
This produces the required results, but I can't seem to get it validated.
It does sort numbers descending, via an exchange type logic, so they just move around without disturbing the order of the words.
Line 45 and 46: These lines reference the String numbers from array, converts them into Integer, and then puts them in an Int ArrayList.
There was an error I encountered while trying to pass the String numbers through isGreaterThan method.
When trying to sort them without changing the type, (1,3,22,0) sorted to (3,22,1,0).
Other test numbers sorted strangely also.
So I changed the type with lines 45 and 46, and changed line 52 to do the comparison.
Lines 53-55, just do the exchange in the original array.
package com.codegym.task.task09.task0930;
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<>();
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) {
// write your code here
ArrayList<Integer> numIndex = new ArrayList<>();
ArrayList<Integer> wordIndex = new ArrayList<>();
ArrayList<Integer> stringToInt = new ArrayList<>();
//Separate ints from words
for (int i = 0; i < array.length; i++) {
if (isNumber(array[i])){
numIndex.add(i);
}
else{
wordIndex.add(i);
}
}
int i,j;
String temp = "";
//Convert Strings to int
for (i = 0; i < numIndex.size(); i++) {
stringToInt.add(Integer.parseInt(array[numIndex.get(i)]));
}
//Loop int to sort descending
for (i = 0; i < numIndex.size()-1; i++) {
for (j = i+1; j < numIndex.size(); j++) {
if (stringToInt.get(j) > stringToInt.get(i)) {
temp = array[numIndex.get(j)];
array[numIndex.get(j)] = array[numIndex.get(i)];
array[numIndex.get(i)] = temp;
}
}
}
//Loop word through isGreaterThan
for (i = 0; i < wordIndex.size()-1; i++) {
for (j = i+1; j < wordIndex.size(); j++) {
if (isGreaterThan(array[wordIndex.get(i)], array[wordIndex.get(j)])) {
temp = array[wordIndex.get(i)];
array[wordIndex.get(i)] = array[wordIndex.get(j)];
array[wordIndex.get(j)] = temp;
}
}
}
}
// 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;
}
}