Java Comparator with insertion sort

Main.java, main class

import java.util.Random;

public class Main {

	public static void main(String args[])
	{

		MyVector<Student> students = getStudents(100);
		
		//sort the students by name, from a to z
		students.sort(new StudentNameComparator());
		System.out.println(students.toString());
		
		//sort the students by id, from smaller id to larger id
		students.sort(new StudentIdComparator());
		System.out.println(students.toString());
		
		
	}
	
	//create size students in a vector
	public static MyVector<Student> getStudents(int size)
	{
		MyVector<Student> students = new MyVector<Student>();
		
		Random random = new Random();
		
		for(int i=0; i<size; i++)
		{
			students.add(new Student(generateRandomName(),10000+random.nextInt(99999) ) );
		}
		
		return students;
	}
	
	//generate a random name
	public static String generateRandomName()
	{
		String alphabet= "abcdefghijklmnopqrstuvwxyz";
		String name = "";
		Random random = new Random();
		int randomLen = 3+random.nextInt(10);
		for (int i = 0; i < randomLen; i++) {
		    char c = alphabet.charAt(random.nextInt(26));
		    name+=c;
		}
		
		return name;
	}
}

Student.java, student class. It will only contain two fields, name and id. We will create a comparator class later for each of them and use it to do the sorting by name and id.

public class Student {

	private String name;
	private int id;

	public Student(String name, int id)
	{
		this.name=name;
		this.id=id;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

    public String toString() {
    	return name+" "+id+"\n";
    }

}

StudentNameComparator.java, student name comparator class.

import java.util.Comparator;

public class StudentNameComparator implements Comparator<Student>{

	@Override
	 // Compare student names
	 // Returns: < 0 if s1 is smaller than s2
	 //			0 if s1 equals s2
	 //			> 0 if s1 is larger than s2
	public int compare(Student s1, Student s2) {
		String name1 = s1.getName().toLowerCase();
		String name2 = s2.getName().toLowerCase();
		
		return name1.compareTo(name2);
	}
}

StudentIdComparator.java, student id comparator class.

import java.util.Comparator;

public class StudentIdComparator implements Comparator<Student>{

	@Override
	 // Compare student Ids
	 // Returns: < 0 if s1 is smaller than s2
	 //			0 if s1 equals s2
	 //			> 0 if s1 is larger than s2
	public int compare(Student s1, Student s2) {
		// TODO Auto-generated method stub
		Integer s1Id=(Integer)s1.getId();
		Integer s2Id=(Integer)s2.getId();
		
		return s1Id.compareTo(s2Id);
	}

}

MyVector.java, extended from Vector class and implementing the insertion sort that takes a comparator and sort the vector by the given comparator.

import java.util.Comparator;
import java.util.Vector;

public class MyVector<E> extends Vector<E> {

	public MyVector()
	{
		super();
	}
	
	// insertion sort
	// pre: c is a valid comparator
	// post: sort this vector in order determined by c
	public void sort(Comparator<E> c)
	{
		int numSorted = 1; // number of values in place
		int index; // general index
		int n = size(); // length of the array
		while (numSorted < n)
		{
			// take the first unsorted value
			E temp = get(numSorted);
			// ...and insert it among the sorted:
			for (index = numSorted; index > 0; index--)
			{
				if (c.compare(temp,get(index-1)) < 0)
				{
					set(index, get(index-1));
				} 
				else 
				{
					break;
				}
			}
			// reinsert value
			set(index, temp);
			numSorted++;
		}
	}
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search