Título: ¿Cómo ordenar un ArrayList de elementos-registro según uno de sus campos? Publicado por: reygecko en 16 Enero 2013, 20:46 pm Hola a todos. :D
Agradecería mucho que alguien me dijera cómo se puede ordenar un ArrayList formado por registros de varios campos. Es decir, si uno de esos campos es numérico, cómo se podría ordenar ese ArrayList para que los elementos estén ordenados de menor a mayor según ese campo numérico. Mi ejemplo: Código: import java.util.*; public class Orden { static class Registro { static String texto; static int numero; static int matriz[][] = new int[2][2]; } public static void main(String[] args) { List<Registro> lista = new ArrayList<>(); Registro registro01 = new Registro(); Registro registro02 = new Registro(); // Doy valores al registro 01 registro01.matriz[0][0] = 5; registro01.matriz[0][1] = 5; registro01.matriz[1][0] = 5; registro01.matriz[0][1] = 5; registro01.texto = "registro01"; registro01.numero = 100; // Doy valores al registro 02 registro02.matriz[0][0] = 1; registro02.matriz[0][0] = 1; registro02.matriz[0][0] = 1; registro02.matriz[0][0] = 1; registro02.texto = "registro02"; registro02.numero = 1; //Añado ambos elementos a la lista lista.add(0,registro01); lista.add(1,registro02); } } Haciéndolo así, el primer elemento del ArrayList sería 'registro01' y el segundo elemento sería 'registro02'. Pues bien, la pregunta es ¿cómo se podrían ordenar de menor a mayor (según el campo registro.numero) los 2 registros que forman el ArrayList? ¿Existe algún método predefinido que lo haga? ¿Y para ordenarlos según, por ejemplo, el valor del elemento (0,0) de las matrices? Yo no doy con ello... :huh: Muchas gracias de antemano y saludos. Título: Re: ¿Cómo ordenar un ArrayList de elementos-registro según uno de sus campos? Publicado por: reygecko en 17 Enero 2013, 00:00 am ¡¡CONSEGUIDO!! :D
He insistido en sobreescribir el método compare() y finalmente lo conseguí. Dejo el código por si sirviese a alguien: Código: import java.util.*; public class Orden { static class Registro { String texto; int numero; int matriz[][] = new int[2][2]; } public static void main(String[] args) { List<Registro> lista = new ArrayList<>(); Registro registro01 = new Registro(); Registro registro02 = new Registro(); Registro registro03 = new Registro(); Registro test = new Registro(); // Doy valores al registro 01 registro01.matriz[0][0] = 1; registro01.matriz[0][1] = 1; registro01.matriz[1][0] = 1; registro01.matriz[0][1] = 1; registro01.texto = "registro01"; registro01.numero = 1; // Doy valores al registro 02 registro02.matriz[0][0] = 2; registro02.matriz[0][0] = 2; registro02.matriz[0][0] = 2; registro02.matriz[0][0] = 2; registro02.texto = "registro02"; registro02.numero = 2; // Doy valores al registro 03 registro03.matriz[0][0] = 3; registro03.matriz[0][0] = 3; registro03.matriz[0][0] = 3; registro03.matriz[0][0] = 3; registro03.texto = "registro03"; registro03.numero = 3; //Añado ambos elementos a la lista lista.add(registro03); lista.add(registro02); lista.add(registro01); test = lista.get(0); System.out.println("El primer registro es: " + test.texto); test = lista.get(1); System.out.println("El segundo registro es: " + test.texto); test = lista.get(2); System.out.println("El tercer registro es: " + test.texto); System.out.println(); System.out.println("Ahora se procede a ordenar los registros"); System.out.println(); Collections.sort(lista, new ordenaRegistros()); test = lista.get(0); System.out.println("El primer registro es: " + test.texto); test = lista.get(1); System.out.println("El segundo registro es: " + test.texto); test = lista.get(2); System.out.println("El tercer registro es: " + test.texto); } } class ordenaRegistros implements Comparator<Orden.Registro> { public int compare(Orden.Registro r1, Orden.Registro r2) { if (r1.numero < r2.numero) { return -1; } else if (r1.numero > r2.numero) { return 1; } else { return 0; } } } De nuevo, millones de gracias a quienes ayudan desinteresadamente a los principiantes como yo. :) |