Well, I think my algo is correct but it doesn't validate 2 things (well I admit, I don't used correctly the "greaterThan" because I found something more efficient)...
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
/*
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) {
// write your code here
//1) Scinder la map en 2: A=seulement chiffres, B = le reste
//2) Tri des map A et B
//3) Fusion des 2 maps
//4) Ne pas oublier le "isGreaterThan()" à mettre quelque part wesh
HashMap<Integer, String> map = new HashMap<>(array.length), onlyNumbers = new HashMap<>(4), onlyStrings = new HashMap<>(4);
int lastMax = 0, indexLastMax = -1, indexAlphaString=-1;
for (int i = 0; i < array.length; i++) {
map.put(i, array[i]);
}
for (Map.Entry<Integer, String> ligne: map.entrySet()
) {
if (isNumber(ligne.getValue())) onlyNumbers.put(ligne.getKey(),ligne.getValue()); //Map A
else
onlyStrings.put(ligne.getKey(), ligne.getValue()); //Map B
}
//2) Tri des 2 maps
ArrayList<Integer> numbersList = new ArrayList<>(); ArrayList<String> stringsList = new ArrayList<>();
for (String chiffre: onlyNumbers.values()
) {
numbersList.add(Integer.parseInt(chiffre));
}
stringsList.addAll(onlyStrings.values());
Collections.sort(numbersList);
Collections.sort(stringsList);
Collections.reverse(numbersList);
int indexList =0;
for (Map.Entry<Integer, String> mapA: onlyNumbers.entrySet()
) {
mapA.setValue(onlyNumbers.get(mapA.getKey()));
onlyNumbers.replace(mapA.getKey(), Integer.toString(numbersList.get(indexList)));
indexList++;
}
indexList=0;
for (Map.Entry<Integer, String> mapB: onlyStrings.entrySet()
) {
mapB.setValue(onlyStrings.get(mapB.getKey()));
onlyStrings.replace(mapB.getKey(), stringsList.get(indexList++));
}
//3) Fusion des 2 maps
map.clear();
map.putAll(onlyNumbers);
map.putAll(onlyStrings);
//4) On intègre tout çaaaaaaaa!!!!
for (int i = 0; i < array.length; i++) {
array[i]=map.get(i);
}
isGreaterThan("a","b"); //le ptit bonus
}
// 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;
}
}