开发者

How to define a Comparator in Java that compares based on two variables

开发者 https://www.devze.com 2023-02-20 16:28 出处:网络
I want to create a comparator to operate such that a process with a lower arrival time will appear first in a sorting, and if two processes have the same arrival time, the one with the lower process i

I want to create a comparator to operate such that a process with a lower arrival time will appear first in a sorting, and if two processes have the same arrival time, the one with the lower process id comes first in the sorting. I tried the following code, but it doesn't seem to be working. Does anyone see a flaw in it?

public class FCFSComparator implements Comparator<Process>
{
    public int compare(Process o1, Process o2)
    {
        开发者_如何转开发int result = o1.getArrivalTime() - o2.getArrivalTime();

        if(result == 0)
        {
            return (o1.getPid() < o2.getPid()) ? -1 : 1;
        }
        else
        {
            return result;
        }
//        return (result != 0 ? result : o1.getPid() - o2.getPid());
    }
}

To be specific, given the processes as follows

pid = 0 arrival time = 10
pid = 1 arrival time = 30
pid = 2 arrival time = 15
pid = 3 arrival time = 15
pid = 4 arrival time = 66

I get the following ordering at the end

Pid = 0 arrival time = 10
Pid = 2 arrival time = 15
Pid = 1 arrival time = 30
Pid = 4 arrival time = 66
Pid = 3 arrival time = 15


I can't find anything wrong with your comparator. Here is my test case:

public class TestComparator {

    static class Process {
        int pid;
        int arrivalTime;

        Process(int pid, int arrivalTime) {
            this.pid = pid;
            this.arrivalTime = arrivalTime;
        }

        @Override
        public String toString() {
            return "Process [pid=" + pid + ", arrivalTime=" + arrivalTime + "]";
        }
    }

    static class FCFSComparator implements Comparator<Process> {
        public int compare(Process o1, Process o2) {
            int result = o1.arrivalTime - o2.arrivalTime;

            if (result == 0) {
                return (o1.pid < o2.pid) ? -1 : 1;
            } else {
                return result;
            }
        }
    }

    public static void main(String[] args) {
        List<Process> processes = Arrays.asList(
                new Process(0, 10),
                new Process(1, 30),
                new Process(2, 15),
                new Process(3, 15),
                new Process(4, 66));

        Collections.sort(processes, new FCFSComparator());

        for (Process process : processes) {
            System.out.println(process);
        }

    }
}

Output:

Process [pid=0, arrivalTime=10]
Process [pid=2, arrivalTime=15]
Process [pid=3, arrivalTime=15]
Process [pid=1, arrivalTime=30]
Process [pid=4, arrivalTime=66]


I'm making that assumption that the things you are comparing are int. In the case of both variables being equal, you are still returning 1 from the inner comparison. Something like this should help:

public class FCFSComparator implements Comparator<Process>
{
    public int compare(Process o1, Process o2)
    {
        int result = o1.getArrivalTime() - o2.getArrivalTime();
        if (result == 0)
        {
            return o1.getPid() - o2.getPid();
        }
        else
        {
            return result;
        }
    }
}

EDIT: I checked the above code and it does output the correct order. I can only assume you have a bug somewhere else in your code.

Pid = 0 arrival time = 10
Pid = 2 arrival time = 15
Pid = 3 arrival time = 15
Pid = 1 arrival time = 30
Pid = 4 arrival time = 66

The full test code is:

import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Main
{
    public static void main(String[] args)
    {
        List<Process> processes = new ArrayList<Process>();
        processes.add(new Process(10, 0));
        processes.add(new Process(30, 1));
        processes.add(new Process(15, 2));
        processes.add(new Process(15, 3));
        processes.add(new Process(66, 4));

        Collections.sort(processes, new FCFSComparator());

        for (Process process : processes)
            System.out.println("Pid = " + process.getPid() + " arrival time = " + process.getArrivalTime());
    }

    static class FCFSComparator implements Comparator<Process>
    {
        public int compare(Process o1, Process o2)
        {
            int result = o1.getArrivalTime() - o2.getArrivalTime();
            if (result == 0)
            {
                return o1.getPid() - o2.getPid();
            }
            else
            {
                return result;
            }
        }
    }

    static class Process
    {
        private int arrivalTime;
        private int pid;

        Process(int arrivalTime, int pid)
        {
            this.arrivalTime = arrivalTime;
            this.pid = pid;
        }

        public int getArrivalTime()
        {
            return arrivalTime;
        }

        public int getPid()
        {
            return pid;
        }
    }
}


I believe you want this:

public class FCFSComparator implements Comparator<Process> {
    public int compare(Process o1, Process o2) {
        if (o1.getArrivalTime() == o2.getArrivalTime()) {
            return (o1.getPid() < o2.getPid()) ? -1 : 1;
        }
        return (o1.getArrivalTime() < o2.getArrivalTime()) ? -1 : 1;
    }
}


The simplest is:

public class FCFSComparator implements Comparator<Process>
{
    public int compare(Process o1, Process o2)
    {
        int timeCmp = Integer.valueOf(o1.getArrivalTime()).compareTo(Integer.valueOf(o2.getArrivalTime()));
        return (timeCmp != 0 ? timeCmp : Integer.valueOf(o1.getPid()).compareTo(Integer.valueOf(o2.getPid())));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消