The value placed in it will be sorted automatically by the key. The default sorting rules are sorted by ascii code. If it is the key, it will not be a problem. If it is the key, it is English and the number will not be a problem. If it is the Chinese
Then there will be problems and you need to write the sorting rules yourself.
Comparator needs to implement classes, for example:
package ;
import ;
import ;
public class CollatorComparator extends Collator {
private Collator collator = ();
private int sort=1;
@Override
public int compare(String arg0, String arg1) {
CollationKey key1 = (arg0);
CollationKey key2 = (arg1);
return sort*(key2);
}
/**
* Set the ordering direction
* @param i sorting direction (positive number: positive order; negative number: reverse order)
*/
public void setSort(int i){
if(i>0){
sort=1;
}else if(i<0){
sort=-1;
}
}
@Override
public CollationKey getCollationKey(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 0;
}
}
Use of comparator:
int sort = 1;///> 0 positive order <0 reverse description
CollatorComparator comparator = new CollatorComparator();
(sort);
Map map = new TreeMap(comparator);
Then, by taking the field to be sorted as the key and the corresponding information as the corresponding value, the sorting can be achieved
Note: The key value cannot be repeated. If the sorting field is repeated, it needs to be processed.
For example: Method 1: When storing data in the map, first determine whether the key to be placed already exists. If it exists, add a distinction mark after the current value.
Method 2: If there is an impossible field in the information, add this unrepeatable field value after the sorting field, and use this combination as a key