按对象的int变量对列表进行排序 [英] Sorting List of Objects by their int variable

查看:62
本文介绍了按对象的int变量对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类(ClassOne),该类具有实例化类(Process)的列表,并且我试图找出如何根据其优先级int对其进行排序的方法.

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

任何排序方法都有效,我想按优先级从最小到最大的顺序对processList中的每个Process对象进行排序.

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

代码:

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

推荐答案

使用Java 8+,您可以在下面尝试以下代码:

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

这篇关于按对象的int变量对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆