input
Cherry
10000
Bob
36
Apple
-220
0
Watermelon
output
Apple
10000
Bob
36
Cherry
0
-220
Watermelon![]()

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<>();
try{
while (true) {
String s = reader.readLine();
if (s.isEmpty()) break;
list.add(s);
}
}
catch(Exception b){
}
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;
}
}