Java, ordene un ArrayList con entradas más largas al final

Tengo un ArrayList<String> en Java. Ahora quiero ordenarlo con algunos requisitos.

Tengo estos artículos en el ArrayList por ejemplo:

 xyz bcd abc_locked cde efg_locked fgh 

Y quiero empujar los que tienen _locked al final y mantener el orden, para hacer esto:

 xyz bcd cde fgh abc_locked efg_locked 

¿Cuál es la mejor manera de hacer esto? ¿Tendría que iterar a través de la lista quitar la cadena y sólo añadir de nuevo? ¿O hay un mejor camino?

Puede probar usar este comparador:

 Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String arg1, String arg2) { if (arg1.matches("^.*_locked$") && arg2.matches("^.*_locked$")) { // Both string have _locked at the end. Retain the order. return 0; } else if (arg1.matches("^.*_locked$")) { // First string have _locked. Swap. return 1; } else if (arg2.matches("^.*_locked$")) { // Second string have _locked. No need to swap return -1; } // None of the string have _locked. Retain the order return 0; } }; Collections.sort(list, comparator); 

Utilice un comparador:

 Collections.sort(list, new Comparator(){ public int compare(String o1, String o2) { if ((o1.endsWith("_locked")&&(!o2.endsWith("_locked"))){ return 1; } else if (!(o1.endsWith("_locked")&&(o2.endsWith("_locked"))){ return 1; } else { //Fallback sorting based on start of string left as exercise to reader } } }); 

Puede intentar usar un comparador parametrizado anónimo, como tal:

 ArrayList<String> myList = new ArrayList<String>(); myList.add("xyz"); myList.add("bcd"); myList.add("abc_locked"); myList.add("cde"); myList.add("efg_locked"); myList.add("fgh"); Collections.sort(myList, new Comparator<String>() { @Override public int compare(String arg0, String arg1) { if (!arg0.contains("_locked") && !arg1.contains("_locked")) { return arg0.compareTo(arg1); } else if (arg0.contains("_locked") && arg1.contains("_locked")) { return arg0.compareTo(arg1); } else if (arg0.contains("_locked")) { return 1; } else { return -1; } }; }); System.out.println(myList); 

Salida:

 [bcd, cde, fgh, xyz, abc_locked, efg_locked] 
 Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String arg1, String arg2) { if (arg1.endsWith("_locked") && arg2.endsWith("_locked")) { return 0; } else if (arg1.endsWith("_locked")) { return 1; } else if (arg2.endsWith("_locked")) { return -1; } return 0; } }; 

Puedes probar esto

 Collections.sort(list, new Comparator() { @Override public int compare(String s1, String s2) { // ascending order return id1.compareTo(id2); // descending order //return id2.compareTo(id1); } }); 

Para una colección de objetos

 /* Here myItems is an arraylist MyItem added randomly MyItem got a property int id This method provide me myItems in ascending order of id's */ Collections.sort(myItems, new Comparator<MyItem>() { @Override public int compare(MyItem lhs, MyItem rhs) { // TODO Auto-generated method stub int lhsId = lhs.getId(); int rhsId = rhs.getId(); return lhsId>rhsId ? 1 : -1; } }); 

Y por supuesto puedes referir esto

  • La mejor manera de reordenar elementos en Android 4 + ListView
  • La columna MediaStore.MediaColumns.TITLE de MediaStore de Android es nula para algunos archivos
  • Cómo mostrar los contactos en orden alfabético en ListView
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.