The rubric says "In a new class, write a static insertion sort method that takes in an array of Comparable objects...In this new class, create a main that tests your code by making an array of TravelGuide objects and passing it into your insertion sort. Also test that your code works properly for an array of Strings as well."

This is what I have:

The compareTo method in TravelGuide :

//compares calling object location to parameter location
	public int compareTo(Object other){	
		
		return (this.location.compareTo(((TravelGuide)other).location));
	}

Insertion sort in other class:

//insertion sort
	public static void insertionSort(Comparable[] array){

		int i, j;
		Comparable newValue;
		for (i = 1; i < array.length; i++) {
			newValue = array[i];
			j = i;
			while (j > 0 && (array[j - 1].compareTo(newValue)>0)) {
				array[j] = array[j - 1];
				j--;
			}
			array[j] = newValue;
		}
	}

I'm very confused...thanks guys.

Recommended Answers

All 2 Replies

It does not work for Strings because of the typecasting in the compareTo method I think

Member Avatar for ztini

You'll want to implement the Comparable interface and create the method compareTo(T o).

Here's a basic example using an Employee class and a EmployeeDirectory class

public class Employee implements Comparable<Employee> {

	private String lastName, firstName, title;
	private int salary;
	
	public Employee(String lastName, String firstName, String title, int salary) {
		this.lastName = lastName;
		this.firstName = firstName;
		this.title = title;
		this.salary = salary;
	}
	
	public int compareTo(Employee that) {
		if (this.salary < that.salary)
			return -1;
		else if (this.salary > that.salary)
			return 1;
		else
			return 0;
	}
	
	public String toString() {
		return lastName + ", " + firstName + ", " + title + ", " + salary;
	}
}
import java.util.ArrayList;


public class EmployeeDirectory {

	private ArrayList<Employee> directory = new ArrayList<Employee>();
	
	public void addEmployee(Employee emp) {
		directory.add(emp);
	}
	
	public void sortBySalary() {
		// Bubble sort
		for (int i = 0; i < directory.size(); i++) {
			for (int j = directory.size() - 1; j > i; j--) {
				if (directory.get(j - 1).compareTo(directory.get(j)) > 0) {
					Employee temp = directory.get(j);
					directory.set(j, directory.get(j - 1));
					directory.set(j - 1, temp);
				}
			}
		}
	}
	
	public void listEmployees() {
		for (Employee emp : directory) 
			System.out.println(emp.toString());
		System.out.println();
	}
	
	public static void main(String[] args) {	
		EmployeeDirectory dir = new EmployeeDirectory();
		
		dir.addEmployee(new Employee("Smith", "John", "Software Engineer", 55000));
		dir.addEmployee(new Employee("Rogers", "Jason", "Project Manager", 75000));
		dir.addEmployee(new Employee("Thompson", "Fred", "Junior Developer", 45000));
		
		dir.listEmployees();
		dir.sortBySalary();
		dir.listEmployees();
	}
}

Console:

Smith, John, Software Engineer, 55000
Rogers, Jason, Project Manager, 75000
Thompson, Fred, Junior Developer, 45000

Thompson, Fred, Junior Developer, 45000
Smith, John, Software Engineer, 55000
Rogers, Jason, Project Manager, 75000

Of course, I used a bubble sort; adjust it for insertion sort---or if you want to impress your teacher, quick sort :)

You can read about the compareTo() method here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.