abok abok - 5 years ago 92
Java Question

compareTo issue with sorting an int array

I have an array of integers that I need to sort in ascending order. I am required to use

compareTo
.

I have the array sorting with one issue. The largest number appears first in the array; the rest are sorted in ascending order.

Driver:
import java.util.Scanner;

public class Driver2 {

public static void main(String[] args) {

int prior,compareResult;
String task;
Task[] taskList = new Task[4];
int [] taskListPrior = new int[4];
Scanner scan = new Scanner(System.in);


System.out.print("Enter priority (between 1 & 10) for breakfast: ");
prior = scan.nextInt();
task = "eatBreakfast";

Task eatBreakfast = new Task(prior,task);
System.out.println("Breakfast priority is: " + eatBreakfast.getPriority());
taskList[0] = eatBreakfast;
taskListPrior[0] = eatBreakfast.getPriority();

System.out.print("Enter priority (between 1 & 10) for lunch: ");
prior = scan.nextInt();
task = "eatLunch";

Task eatLunch = new Task(prior,task);
System.out.println("Lunch priority is: " + eatLunch.getPriority());
taskList[1] = eatLunch;
taskListPrior[1] = eatLunch.getPriority();

System.out.print("Enter priority (between 1 & 10) for dinner: ");
prior = scan.nextInt();
task = "eatDinner";

Task eatDinner = new Task(prior,task);
System.out.println("Dinner priority is: " + eatDinner.getPriority());
taskList[2] = eatDinner;
taskListPrior[2] = eatDinner.getPriority();

System.out.print("Enter priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
task = "haveNap";

Task haveNap = new Task(prior,task);
System.out.println("Nap priority is: " + haveNap.getPriority());
taskList[3] = haveNap;
taskListPrior[3] = haveNap.getPriority();

System.out.println("--------------");

System.out.println("Printing task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}

System.out.print("Enter new priority (between 1 & 10) for nap: ");
prior = scan.nextInt();
haveNap.setPriority(prior);
System.out.println("New priority for nap is: " + haveNap.getPriority());
taskList[3] = haveNap;
taskListPrior[3] = haveNap.getPriority();
System.out.println("--------------");

System.out.println("Printing task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}

Task tempTask;
int tempTaskPrior;
for(int current = 0 ; current < taskList.length ; current++)
{
for(int i = 1 ; i < (taskList.length) ; i++)
{
compareResult = (taskList[current]).compareTo(taskList[i]);
if(compareResult == 1){
tempTask = (taskList[i]);
tempTaskPrior = (taskListPrior[i]);
taskList[i] = (taskList[current]);
taskListPrior[i] = (taskListPrior[current]);
taskList[current] = tempTask;
taskListPrior[current] = tempTaskPrior;
}
}
}

System.out.println("Printing sorted list of task names and priorities:");
for(int p = 0 ; p < taskListPrior.length ; p++)
{
System.out.println(taskListPrior[p] + " - " + taskList[p]);
}

}

}


Task class:
public class Task implements Priority, Comparable{
public int prior,priorityQuery,priorityNum;
public String task,taskItem,fullTask;
Task taskName;

public Task(int priorityQuery, String taskQuery){
task = taskQuery;
if (priorityQuery >= minPriority && priorityQuery <= maxPriority)
prior = priorityQuery;
else
System.out.println("Out of valid range, task will have no priority");
}

public void setPriority(int priority)
{
if (priority >= minPriority && priority <= maxPriority)
prior = priority;
else
System.out.println("Out of valid range, task will have no priority");
}

public int getPriority()
{
return prior;
}

public String toString(){
taskItem = task;

return taskItem;
}

public Task getTaskName(){
return taskName;
}

public int compareTo(Task compTask){
if (this.getPriority() < compTask.getPriority())
return 1;
else if (this.getPriority() > compTask.getPriority())
return -1;
else
return 0;

}

}


There are actually two parallel arrays, the one used for the
compareTo
method consists only of integers.

Answer Source

There's something wrong with that sort routine. You start the inner loop from 1 every time so current may be > than i. So it will (in some circumstances) swap items that are in place to out of place.

The proper answer is to use java.util.Arrays.sort().

If you're interested in sorting algorithms write a stand-alone class and implement a unit test script to make sure it works before embedding it your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download