All rights reserved. All rights reserved.
Welcome reprint, please indicate the source when reproduced:
http://blog.csdn.net/xiaofei_it/article/details/51399159
The company's Android projects often need to sort the elements, and the sorting is multi-field, the corresponding comparator is more difficult to write. So I wrote a tool class that allows the user to automatically generate the corresponding comparator by specifying the collation.
Project Address:
Https://github.com/Xiaofei-it/ComparatorGenerator
Characteristics
Java programming often needs to sort the data and write a comparator. But comparator is more difficult to write, especially when multi-field sorting is more complex.
You can easily use this tool class to generate comparator. You only need to specify which fields and ascending or descending order you want to sort by, and the tool class will generate the corresponding comparator for you.
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
Now suppose you want to sort many instances of a class. The sort is based on the return value of some fields or functions.
So all you have to do is tell the tool class these fields and functions, and specify the weights and the ascending order.
There are two ways to specify a sorting strategy: using annotations or using APIs.
Using annotations
Now you want to generate comparator for Item1. When sorting, the price is first ascending, if the price is the same, the distance is descending, and if distance is the same, the ItemName is ascending.
Then add annotations to the corresponding fields of the ITEM1:
class Item1 {@Criterion (priority = 3, order = order.ascending) private int price; @Criterion (priority = 2, order = order.descending) private int distance; @Criterion (priority = 1)//order default is ascending private String itemname;}
Just write it when you use it:
Comparator<Item1> comparator = new ComparatorGenerator<Item1>(Item1.class).generate();Arrays.sort(items1, comparator);
Let's look at an example. Sort the Item3, first by the total price ascending, and then by ItemName Ascending. The total price is given by the function, and we can still add annotations to the function.
Note: The function must be an argument-free member function!
class Item3 {private int price; private int number; @Criterion (priority = 1)//default ascending private String itemname; @Criterion (priority = 2, order = order.ascending) int Gettotalprice () {return price * number; }}
Write the following code when you use it:
Comparator<Item3> comparator = new ComparatorGenerator<Item3>(Item3.class).generate();Arrays.sort(items3, comparator);
Using the API
There are times when you want to write different comparator for a class, such as ordering the order in many ways: distance priority, evaluation priority, and price priority.
Annotations cannot be used at this time because only comparator can be generated by annotations.
This is then specified directly using the API.
The following is a class Item2.
class Item2 { private int price; private int distance; private String itemName;}
First to price ascending, then to distance descending, then to ItemName Ascending.
comparator<item2> Comparator2 = new Comparatorgenerator<item2> (Item2.class) . addcriterion (3, "price", order.ascending). Addcriterion (2, "distance", order.descending). Addcriterion (1, " ItemName ")//default ascending. Generate (); Arrays.sort (ITEMS2, Comparator2);
Let's look at an example.
class Item4 { private int price; private int number; private int distance; int getTotalPrice() { return price * number; }}
sort Item4, first by Total price ascending, then by distance descending.
Comparator<Item4> comparator4 = new ComparatorGenerator<Item4>(Item4.class) .addCriterion(3, "getTotalPrice", Order.ASCENDING) .addCriterion(2, "distance", Order.DESCENDING) .generate();Arrays.sort(items4, comparator4);
Are you hungry? Open Source project: Java Comparator generator