The Zombie Sort Test

A great little exercise is implementing a sort comparator.

The problem is this:

  1. Goal:  Write a concrete implementation to an interface “Comparator”  that sorts an object Zombie by Zombie’s last name property.  Comparator has a method “compare” that you will override with your implementation.
  2. The  output is a new list sorted in alphabetical order by last name, ascending.
  3. Provided are the Zombie object and the Service, and any information about Comparator.

I fired up my Eclipse editor and wrote it in about 20 minutes. I always use JUnit as a way to run the code — it’s a much better technique I feel than writing a main for this fun little test.

//First the main entity class
package main;

public class Zombie {

	private String firstName;
	private String lastName;

	public Zombie(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

//Here's the service you will use to test your concrete comparator
package main;

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

public class ZombieService {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void sortByLastName(List zombies) {
		Comparator comp = new ZombieComparator();
		Collections.sort(zombies, comp);
		printZombie(zombies);
	}

	private void printZombie(List zombies) {
		for (Zombie z : zombies) {
			System.out.println(z.getLastName());
		}
	}
}

//here's the unit test
package test;

import java.util.ArrayList;
import java.util.List;

import main.Zombie;
import main.ZombieService;

import org.junit.Test;

public class ZombieServiceTest {

	@Test
	public void test() {
		List zombieList = new ArrayList();
		zombieList.add(new Zombie("Fred", "Munster"));
		zombieList.add(new Zombie("Count", "Dracula"));
		zombieList.add(new Zombie("Lilly", "Adams"));

		ZombieService zombieService = new ZombieService();
		zombieService.sortByLastName(zombieList);
	}

}


//Here's the comparator implementation  THIS IS THE SOLUTION!
package main;

import java.util.Comparator;

@SuppressWarnings("rawtypes")
public class ZombieComparator implements Comparator {

	@Override
	public int compare(Object arg0, Object arg1) {
		Zombie o0 = (Zombie) arg0;
		Zombie o1 = (Zombie) arg1;
		return o0.getLastName().compareTo(o1.getLastName());
	}
}

The output:

Adams
Dracula
Munster


I am thinking of doing more algorithm studies at a code meetup group I started, which would focus on the fundamental problem solving skills which we may or may not use at work. The idea would be to bring any compiler (maybe groovy, or clojure, ruby, pearl etc.). How about solving the fizzbuzz problem in SQL? What about Fibonacci with QBasic? Many languages, many people, many approaches, many funs! I’ll give it some thought …

I added the project to Bitbucket in the Zombie folder and its Eclipse files:

Zombie

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>