Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FontAlphabetizer
- implements Comparator<Font> {
- @Override
- public int compare(Font font1, Font font2) {
- return font1.getName().compareTo(font2.getName());
- }
- }
- class MapValueComparator<K, V extends Comparable<V>>
- implements Comparator<Map<K, V>> {
- final K key;
- MapValueComparator(K key) {
- this.key = key;
- }
- @Override
- public int compare(Map<K, V> map1, Map<K, V> map2) {
- return map1.get(key).compareTo(map2.get(key));
- }
- }
- static void sortByTown(List<Map<String, String>> list) {
- Collections.sort(list, new MapValueComparator<String, String>("town"));
- }
- class PopulationComparator
- implements Comparator<Map<String, String>> {
- @Override
- public int compare(Map<String, String> map1, Map<String, String> map2) {
- final Long pop1 = Long.valueOf(map1.get("population"));
- final Long pop2 = Long.valueOf(map2.get("population"));
- return pop1.compareTo(pop2);
- }
- }
- public class CustomComparator implements Comparator<MyObjectType>
- {
- public int compare(MyObjectType ob1 , MyObjectType ob2)
- {
- //code to compare the 2 objects
- }
- }
- final List<Map<String, Object>> towns = new ArrayList<Map<String, Object>>();
- final Map<String, Object> toronto = new HashMap<String, Object>();
- toronto.put("town", "Toronto");
- toronto.put("population", 2500000);
- toronto.put("age", 147);
- towns.add(toronto);
- final Map<String, Object> ottawa = new HashMap<String, Object>();
- ottawa.put("town", "Ottawa");
- ottawa.put("population", 883000);
- ottawa.put("age", 159);
- towns.add(ottawa);
- final Map<String, Object> montreal = new HashMap<String, Object>();
- montreal.put("town", "Montreal");
- montreal.put("population", 1600000);
- montreal.put("age", 372);
- towns.add(montreal);
- final Map<String, Object> quebec = new HashMap<String, Object>();
- quebec.put("town", "Quebec City");
- quebec.put("population", 600000);
- quebec.put("age", 406);
- towns.add(quebec);
- final Map<String, Object> vancouver = new HashMap<String, Object>();
- vancouver.put("town", "Vancouver");
- vancouver.put("population", 600000);
- vancouver.put("age", 128);
- towns.add(vancouver);
- Collections.sort(towns, new Comparator<Map<String, Object>>() {
- @Override
- public int compare(final Map<String, Object> o1, final Map<String, Object> o2) {
- if (o1.get("population") instanceof Integer && o2.get("population") instanceof Integer && !((Integer)o1.get("population")).equals((Integer)o2.get("population"))) {
- return ((Integer)o1.get("population")).compareTo((Integer)o2.get("population"));
- }
- if (o1.get("age") instanceof Integer && o2.get("age") instanceof Integer) {
- return ((Integer)o1.get("age")).compareTo((Integer)o2.get("age"));
- }
- // Default if there is no population/no age, shouldn't happen.
- // TODO : do something else.
- return o1.toString().compareTo(o2.toString());
- }
- });
- for (final Map<String, Object> town: towns) {
- System.out.println(town.get("population")+"t"+town.get("age")+"t"+town.get("town"));
- }
- 600000 128 Vancouver
- 600000 406 Quebec City
- 883000 159 Ottawa
- 1600000 372 Montreal
- 2500000 147 Toronto
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement