I want to know a simple method for sorting a LinkedList! I'm breaking my head and rolling the internet, but I only find methods that already perform the work I WOULD perform manually.
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 for loop and perhaps IFS conditions sort the list in ascending order?
I know that for ordering I will have to do ifs for comparison of the major and minor, but I wanted help on how to pass these values and how to work with them in the same class.
Thank you in advance.