简体   繁体   中英

Sorting List of Objects by their int variable

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 ClassOne
{
    static List<Process> processList = new ArrayList<Process>();

    public static void main(String[] args)
    {
        //hardcoded for example
        processList.add(new Process(3));
        processList.add(new Process(1));
        processList.add(new Process(2));
        
        The processes are current not ordered in the List by priority, so I call insertion sort
    }

    //Im pretty sure this is changing their priority instead of where they are in the List, but i dont know how to change it
    public static void InsertionSort()
    {
        int n = processList.size(); 
        for (int i = 1; i < n; ++i) 
        { 
            int key = processList.get(i).priority; 
            int j = i - 1; 
 
            /* Move elements of processList.get(0..i-1]).priority, that are 
               greater than key, to one position ahead 
               of their current position */
            while (j >= 0 && processList.get(j).priority > key)
            {
                processList.get(j+1).priority = processList.get(j).priority; 
                j = j - 1; 
            } 
            processList.get(j + 1).priority = key; 
        }

}

public class Process
{
    int priority;
    public Process(int tempPriority)
    {
        priority = tempPriority;
    }
}

any sorting method works, I want want to sort each Process object in processList by their priority, from least to greatest.

code after trying first solution:

public static void InsertionSort()
    {
         System.out.println(processList.get(0).name);
         System.out.println(processList.get(1).name);
        int n = processList.size(); 
        for (int i = 1; i < n; ++i) 
        { 
            int key = processList.get(i).priority; 
            int j = i - 1; 
            //The method set(int, Process) in the type List<Process> is not applicable for the arguments (int, int)
            /* Move elements of processList.get(0..i-1]).priority, that are 
               greater than key, to one position ahead 
               of their current position */
            while (j >= 0 && processList.get(j).priority > key)
            {
                processList.set(j + 1, processList.get(j));
                j = j - 1; 
            } 
            processList.set(j + 1, processList.get(i));
            System.out.println("Queue Sorted");
            System.out.println(processList.get(0).name);
            System.out.println(processList.get(1).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));

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM