0

I'm taking in all the information from an MySQL database table using a resultset and adding all the values into an array

public void populateQueueFromDB() {
        // create priority queue
        try {
            String url = "jdbc:mysql://localhost:3306/project";
            Connection conn = DriverManager.getConnection(url, "root", "nbuser");

            PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication,priority,cores,disk_space,analysis FROM booking");
            ResultSet rs;
            rs = stmt.executeQuery();


            //List<JobRequest> jobList = new ArrayList<JobRequest>();

            while (rs.next()) {
                JobRequest job = new JobRequest();
                User user = new User();
                user.setUserID(rs.getString("user_id"));
                job.setUserID(user.getUserID()); // changes the /user id to the job.setuser id so can call for my queue print.
                job.setStartDate(rs.getString("s_date"));
                job.setEndDate(rs.getString("e_date"));
                job.setDeadDate(rs.getString("d_date"));
                job.setDepartment(rs.getString("department"));
                job.setProjectName(rs.getString("projectname"));
                job.setProjectApplication(rs.getString("projectapplication"));
                job.setPriority(rs.getInt("priority"));
                job.setCores(rs.getInt("cores"));
                job.setDiskSpace(rs.getInt("disk_space"));
                job.setAnalysis(rs.getString("analysis"));

                schedulerPriorityQueue.addJob( job );

            }
            schedulerPriorityQueue.printQueue();

            conn.close();

        } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }

    }

from here I go off and call my comparator to order the data, depending on priority of being either 1,2,3 then sort the queue. some other bits of code naming etc but essentially it sends me to the comparator

public class JobQueueComparator implements Comparator<JobRequest> {

    @Override
    public int compare(JobRequest object1, JobRequest object2) {
        if(object1.getPriority() < object2.getPriority()){
            return 1;
        } else {
            return -1;
        }
    }

}

But the output I'm getting from the comparator is ordering it in priority 3, 1 then 2. I've adapted this from example online but I don't understand the returns on comparator examples I've seen.

How would I be able to change that comparator to sort my priorities, 1 being the most important, 3 being the least. I made sure I'm printing out the output after adding all the result sets into the array so I know it's working as it's changed my ordering around, just dont know how to order it how I want.

Thanks

EDIT: schedulerPriorityQueue

public class Queue {

    private Comparator<JobRequest> comparator = new JobQueueComparator(); //calls my comparator
    private PriorityQueue< JobRequest> scheduledJobs = new PriorityQueue<JobRequest>(100, comparator);

    public void addJob(JobRequest job) {
        // now add job to priority queue
        scheduledJobs.add(job); // add jobs from the resultset into queue
    }
4
  • You obviously need three cases: prio1 < prio2, prio1 == prio2, else. Commented Feb 14, 2013 at 13:33
  • Since the priority field is an int, why not just cast to an Integer object and use its compareTo method (Integer is Comparable)? Commented Feb 14, 2013 at 13:35
  • you don't show how your comparator is used... the first block of code is totally worthless for the matter of the question. You don't show how schedulerPriorityQueue.addJob() is implemented. That is probably where your error is located. Nor we know if somewhere else you call any sort... Commented Feb 14, 2013 at 13:35
  • see the EDIT to where the schedulerpriorityQueue.addjob() is implemented Commented Feb 14, 2013 at 14:42

4 Answers 4

2

Make it easy on yourself and use Integer and its compareTo method.

Your comparator method would look like this

    @Override
public int compare(JobRequest object1, JobRequest object2) {
    Integer iO1 = Integer.valueOf(object1.getPriority());
    Integer iO2 = Integer.valueOf(object2.getPriority());
    return -(i01.compareTo(iO2));
}

Assuming getPriorityreturns a int or String.

Sign up to request clarification or add additional context in comments.

3 Comments

it sorts my output but it doesnt keep to the rules so it would go 3,3,2,1,2,1,1 so their is a slight issue somewhere why it wouldn't go 3,3,2,2,1,1, ?
Hmm thats odd, I add more information to the database then it sorts it self out and orders it 3,2,1 any thoughts?
This should work regardless of the number of items to compare. I don't see why having more or less makes a difference. The only thing that might change after ordering is the priority within JobRequest which would then make it appears as the order wasn't correct after insertion.
0

Simply swap the signs on the compare method. This will reverse the order.

public int compare(JobRequest object1, JobRequest object2) {
    if(object1.getPriority() < object2.getPriority()){
        return -1;
    } else {
        return +1;
    }
}

Comments

0

You should return 0 from the comparator if the two objects are equal, an integer less than 0 if object 1 < object 2, and an integer greater than 0 if object 1 > object 2. Currently the JobQueueComparator never returns 0. Try the following:

public class JobQueueComparator implements Comparator<JobRequest> {
    @Override
    public int compare(JobRequest object1, JobRequest object2) {
        return object1.getPriority() - object2.getPriority();
    }
}

See the Comparator documentation for more details.

Comments

0

You are missing the case where the priorities are equal. I would use something like this:

public class JobQueueComparator implements Comparator<JobRequest> {

@Override
public int compare(JobRequest object1, JobRequest object2) {
        return -(object1.getPriority() - object2.getPriority());
 }
}

This would work if you take into consideration the priority order like this: 1, 2, 3.

1 Comment

missing a return statement, what would I need to put in place their as editted in your comment above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.