I have a class (ClassOne) that has a list of instanced Classes(Process) and i\'m trying to figure out how to sort them based on their priority int.
public class C
with java 8+ you can try this code below:
import java.util.*;
import java.util.stream.Collectors;
public static void InsertionSort()
{
System.out.println(processList.get(0).name);
System.out.println(processList.get(1).name);
System.out.println(processList.get(2).name);
processList=processList.stream()
.sorted((o1,o2)->{return o1.priority-o2.priority;})
.collect(Collectors.toList());
System.out.println(processList.get(0).name);
System.out.println(processList.get(1).name);
System.out.println(processList.get(2).name);
}
Use List#set to set an element at an index.
processList.get(j+1).priority = processList.get(j).priority;
becomes
processList.set(j+1, processList.get(j));
And
processList.get(j + 1).priority = key;
becomes
processList.set(j+1, processList.get(i));