Ver Mensaje Individual
  #10 (permalink)  
Antiguo 21/10/2014, 06:02
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: Ordenar ArrayList

Es que hay que currase un poco el comparador.

Aqui te pongo uno sobre tu codigo. No lo he probado y lo mismo ni compila (lo he hecho en el notepad) pero te puede dar una idea



Código Java:
Ver original
  1. public class Prueba {
  2.  
  3.     public static void main(String[] args) {
  4.         // TODO Auto-generated method stub
  5.         List<String> lista = new ArrayList<>();
  6.  
  7.         //solo letras minusculas
  8.         lista.add("12a");
  9.         lista.add("acd");
  10.         lista.add("a%c");
  11.         lista.add("bdc");
  12.         lista.add("bc");//las contrasenas no tienen que ser del mismo tamano
  13.  
  14.         Collections.sort(lista, new Ordenar());
  15.  
  16.         System.out.print(lista);
  17.     }
  18. }
  19.  
  20. class Ordenar implements Comparator<String> {
  21.  
  22.     @Override
  23.     public int compare(String o1, String o2) {
  24.         if (o1 == null && o2 == null) {
  25.             return 0;
  26.         } else if (o2 == null) {
  27.             return -1;
  28.         } else if (o1 == null) {
  29.             return 1;
  30.         }
  31.        
  32.         //Select shortest to pivot on
  33.         int shortest = (o1.length() <= o2.length()) ? o1.length() : o2.length();
  34.  
  35.         for (int i = 0; i < shortest; i++) {
  36.             char charO1 = o1.charAt(i);
  37.             char charO2 = o2.charAt(i);
  38.  
  39.             if (Character.isLetter(charO1)) {
  40.                 if (!Character.isLetter(charO2)) {
  41.                     return -1;
  42.                 } else {
  43.                     int comp = Character.compare(charO1, charO2);
  44.                     if (comp != 0) {
  45.                         return comp;
  46.                     }
  47.                 }
  48.             } else if (Character.isDigit(charO1)) {
  49.                 if (Character.isLetter(charO2)) {
  50.                     return 1;
  51.                 } else if (Character.isDigit(charO2)) {
  52.                     int comp = Character.compare(charO1, charO2);
  53.                     if (comp != 0) {
  54.                         return comp;
  55.                     }
  56.                 } else {
  57.                     return -1;
  58.                 }
  59.             } else {
  60.                 if (Character.isLetter(charO2) || Character.isDigit(charO2)) {
  61.                     return 1;                    
  62.                 } else {
  63.                     int comp = Character.compare(charO1, charO2);
  64.                     if (comp != 0) {
  65.                         return comp;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         return (o1.length() <= o2.length()) ? -1 : 1;
  72.     }
  73. }


Un saludo
__________________
If to err is human, then programmers are the most human of us