Created
February 10, 2013 12:40
-
-
Save shaik2many/4749475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ | |
*/ | |
//1. Sort an Array | |
String[] fruits = new String[] {"Pineapple","Apple", "Orange", "Banana"}; | |
Arrays.sort(fruits); | |
int i=0; | |
for(String temp: fruits){ | |
System.out.println("fruits " + ++i + " : " + temp); | |
} | |
//2. Sort an ArrayList | |
List<String> fruits = new ArrayList<String>(); | |
fruits.add("Pineapple"); | |
fruits.add("Apple"); | |
fruits.add("Orange"); | |
fruits.add("Banana"); | |
Collections.sort(fruits); | |
int i=0; | |
for(String temp: fruits){ | |
System.out.println("fruits " + ++i + " : " + temp); | |
} | |
//3. Sort an Object with Comparable | |
public class Fruit implements Comparable<Fruit> | |
private String fruitName; | |
private String fruitDesc; | |
private int quantity; | |
public int compareTo(Fruit compareFruit) { | |
int compareQuantity = ((Fruit) compareFruit).getQuantity(); | |
//ascending order | |
return this.quantity - compareQuantity; | |
//descending order | |
//return compareQuantity - this.quantity; | |
} | |
} | |
public class SortFruitObject{ | |
public static void main(String args[]){ | |
List<Fruit> fruits = new ArrayList<Fruit>(); | |
Collections.sort(fruits); | |
int i=0; | |
for(Fruit temp: fruits){ | |
System.out.println("fruits " + ++i + " : " + temp.getFruitName() + | |
", Quantity : " + temp.getQuantity()); | |
} | |
} | |
} | |
//4. Sort an Object with Comparator | |
//How about sorting with Fruit’s “fruitName” or “Quantity”? The Comparable interface is only allow to | |
//sort a single property. To sort with multiple properties, you need Comparator | |
//See the new updated Fruit class again | |
public class Fruit implements Comparable<Fruit>{ | |
private String fruitName; | |
private String fruitDesc; | |
private int quantity; | |
public int compareTo(Fruit compareFruit) { | |
int compareQuantity = ((Fruit) compareFruit).getQuantity(); | |
//ascending order | |
return this.quantity - compareQuantity; | |
//descending order | |
//return compareQuantity - this.quantity; | |
} | |
public static Comparator<Fruit> FruitNameComparator | |
= new Comparator<Fruit>() { | |
public int compare(Fruit fruit1, Fruit fruit2) { | |
String fruitName1 = fruit1.getFruitName().toUpperCase(); | |
String fruitName2 = fruit2.getFruitName().toUpperCase(); | |
//ascending order | |
return fruitName1.compareTo(fruitName2); | |
//descending order | |
//return fruitName2.compareTo(fruitName1); | |
} | |
}; | |
} | |
public static void main(String[] arg){ | |
Arrays.sort(fruits, Fruit.FruitNameComparator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment