Java Object Sorting - Comparable and Comparator

Comparable is an internal comparator and a sorting interface. A class that implements the Comparable interface can compare the size with itself and indicates that the implementation class supports sorting. The specific size comparison depends on the implementation of the compareTo() method in Comparable. The interface implementation:

package com.luna.model.strategy;
public interface Comparable {
	public int compareTo(Object o);
}

Comparator is an external comparator, which is a typical implementation of a strategy pattern. When the two classes you want to compare do not implement the Comparable interface or the compareTo() method does not support the comparison method you want, you can use the Comparator and the Comparator interface to implement:

package com.luna.model.strategy;
public interface Comparator {
	int compare(Object o1,Object o2);
}

Comparable's concrete implementation class Cat:

package com.luna.model.strategy;
public class Cat implements Comparable{
	private String name;
	private int height;	
	private int weight;	
	private Comparator comparator = new CatWeightComparator(); //Use the external comparator Comparator to implement the compareTo method of the Comparable interface
	public Comparator getComparator() {
		return comparator;
	}
	public void setComparator(Comparator comparator) {
		this.comparator = comparator;
	}
	public Cat() {
		super();
	}
	public Cat(String name, int height, int weight) {
		super();
		this.name = name;
		this.height = height;
		this.weight = weight;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + ", height=" + height + ", weight=" + weight + "]";
	}	
	@Override
	public int compareTo(Object o) {
		return comparator.compare(this, o);
	}
}

Comparable's concrete implementation class Dog:

package com.luna.model.strategy;
public class Dog implements Comparable{
	private String name;
	private int height;	
	private int weight;	
	public Dog() {
		super();
	}
	public Dog(String name, int height, int weight) {
		super();
		this.name = name;
		this.height = height;
		this.weight = weight;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + ", height=" + height + ", weight=" + weight + "]";
	}
	@Override
	public int compareTo(Object o) { //Implement the compareTo() method of the Comparable interface by myself
		if(o instanceof Dog){
			Dog d = (Dog)o;
			if(this.getHeight()>d.getHeight()) return 1;
			else if(this.getHeight()<d.getHeight()) return -1;
			else return 0;
		}else{
			return -100;
		}
	}
}

The concrete implementation class of Comparator CatWeightComparator:

package com.luna.model.strategy;
public class CatWeightComparator implements Comparator{
	@Override
	public int compare(Object o1, Object o2) {
		Cat c1 = (Cat)o1;
		Cat c2 = (Cat)o2;
		if(c1.getWeight()>c2.getWeight()) return -1;
		else if(c1.getWeight()<c2.getWeight()) return 1;
		return 0;
	}
}

Object sorting tool class DataSorter:

package com.luna.model.strategy;
public class DataSorter {
	public static void sort(Object[] o){
		/**
		 * Bubble sort: sort from back to front
		 */
		for (int i = o.length; i>0; i--) {
			for (int j = 0; j < i-1; j++) {
				Comparable o1 = (Comparable)o[j];
				Comparable o2 = (Comparable)o[j+1];
				if(o1.compareTo(o2)==1){
					swap(o, j, j+1);
				}
			}
		}	
		/**
		 * Bubble sort: sort from front to back
		 */
		for (int i = 0; i < o.length - 1; i++) {
            for (int j = 0; j < o.length - 1 - i; j++) {	
				Comparable o1 = (Comparable)o[j];
				Comparable o2 = (Comparable)o[j+1];
				if(o1.compareTo(o2)==1){
					swap(o, j, j+1);
				}
			}
		}
	}
	private static void swap(Object[] a, int x, int y) {
		Object temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}
	public static void sort(Cat[] a){
		for (int i = a.length; i>0; i--) {
			for (int j = 0; j < i-1; j++) {
				if(a[j].getHeight()>a[j+1].getHeight()){
					swap(a,j,j+1);
				}
			}
		}
	}	
	private static void swap(Cat[] a, int x, int y) {
		Cat temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}
	/**
	 * Bubble sort algorithm
	 * @param a
	 */
	public static void sort(int[] a){
		for (int i = a.length; i>0; i--) {
			for (int j = 0; j < i-1; j++) {
				if(a[j]>a[j+1]){
					swap(a,j,j+1);
				}
			}
		}
	}	
	private static void swap(int[] a, int x, int y) {
		int temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}
	public static void print(int[] a){
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	public static void print(Cat[] c) {
		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i]+"");
		}
		System.out.println();
	}	
	public static void print(Object[] c) {
		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i]+"");
		}
		System.out.println();
	}
}

Sort test class Test:

package com.luna.model.strategy;
public class Test {
	public static void main(String[] args) {
		int a[] = {9,5,3,7,1};
		DataSorter.sort(a);
		DataSorter.print(a);	
		Cat c[] = {new Cat("Red",5,5),new Cat("blue",3,3), new Cat("pink",1,1)};
		DataSorter.sort(c);
		DataSorter.print(c);
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723322&siteId=291194637