An easy-to-use helper class for generating a comparator for a specified class. Useful when sorting the instances of the specified class.
##Features
A comparator is needed when sorting a list of elements of a specified class. But it is difficult to write a comparator, especially in multiple-criteria sorting.
This helper class makes it easy for you to obtain a comparator. What you need to do is just specifying some criteria and then the helper class will give you a comparator.
##Gradle
dependencies {
compile 'xiaofei.library:comparator-generator:1.1'
}
##Maven
<dependency>
<groupId>xiaofei.library</groupId>
<artifactId>comparator-generator</artifactId>
<version>1.1</version>
<type>pom</type>
</dependency>
##Usage
Often, elements are sorted by the values of some specified fields or the return values of some specified methods. A field or a method has a corresponding criterion. A criterion has a priority. The field or the method with a higher priority will be considered first when sorting. A criterion has also a sorting order, which, by default, is ascending.
There are two ways to specify criteria: Using annotations and using APIs.
Take the following class as an example:
class Item1 {
@Criterion(priority = 3, order = Order.ASCENDING)
private int price;
@Criterion(priority = 2, order = Order.DESCENDING)
private int distance;
@Criterion(priority = 1) // The default sorting order is Order.ASCENDING.
private String itemName;
}
Suppose there is an array named items1. Then we can sort the array as the following shows:
Comparator<Item1> comparator = new ComparatorGenerator<Item1>(Item1.class).generate();
Arrays.sort(items1, comparator);
Here is another example:
class Item3 {
private int price;
private int number;
private String itemName;
@Criterion(priority = 2, order = Order.ASCENDING)
int getTotalPrice() {
return price * number;
}
@Criterion(priority = 1) // The default sorting order is Order.ASCENDING.
String getItemName() {
return itemName;
}
}
Suppose there is an array named items3. Then we can sort the array as the following shows:
Comparator<Item3> comparator = new ComparatorGenerator<Item3>(Item3.class).generate();
Arrays.sort(items3, comparator);
Take the following class as an example:
class Item2 {
private int price;
private int distance;
private String itemName;
}
Suppose there is an array named items2. Then we can sort the array as the following shows:
Comparator<Item2> comparator2 =
new ComparatorGenerator<Item2>(Item2.class)
.addCriterion(3, "price", Order.ASCENDING)
.addCriterion(2, "distance", Order.DESCENDING)
.addCriterion(1, "itemName") // The default sorting order is Order.ASCENDING.
.generate();
Arrays.sort(items2, comparator2);
Here is another example:
class Item4 {
private int price;
private int number;
private int distance;
int getTotalPrice() {
return price * number;
}
}
Suppose there is an array named items4. Then we can sort the array as the following shows:
Comparator<Item4> comparator4 =
new ComparatorGenerator<Item4>(Item4.class)
.addCriterion(3, "getTotalPrice", Order.ASCENDING)
.addCriterion(2, "distance", Order.DESCENDING)
.generate();
Arrays.sort(items4, comparator4);