2
I want to know a simple method to order a Linkedlist! I’m breaking my head and searching the Internet, but I just find methods that already perform the work that I WOULD LIKE manually perform.
public class OrdenacaoTeste {
public static void ordenaLista(LinkedList<Integer> lista) {
}
public static void main(String[] args) {
LinkedList<Integer> lista = new LinkedList<Integer>();
lista.add(1);
lista.add(5);
lista.add(3);
lista.add(6);
lista.add(8);
System.out.println("lista não ordenada: " + lista);
Collections.sort(lista); //ordena automaticamente
System.out.println(lista);
for (int i = 0; i < lista.size(); i++) {
for (int j = 0; j < lista.size() - 1; j++) {
}
}
}
}
My class gets the values, 1, 5, 3, 6, 8! I know how a linked list works, so I’m using the linkedlist to save work! But how can I as a loop for and perhaps conditions IFS order the list in ascending order?
I know that for ordering I will have to make ifs to compare the bigger and smaller, but wanted help on how to pass these values and how to work with them in the same class.
Thanks in advance.
http://stackoverflow.com/a/19263295/5524514
– user28595
I do not see how this mode will help me to carry out the task manually?!
– Guto G
Your question gives to understand what you want to do manually. If that’s not what you want, I suggest you edit it and make what you want to make clear.
– user28595
I believe the question is very clear, sorry for any inconvenience, I just want to order the values of this list in an increasing and manual way. For example, if it were a 5 position vector, I would do two for’s and one if(vet[j] > vet[j + 1]){ aux = vet[j]; vet[j] = vet[j+1]; vet[j+1] = aux; }.
– Guto G
I understand, I think this one will suit you right http://stackoverflow.com/a/6369923/5524514
– user28595
Thanks @diegofm for their patience and comments!
– Guto G