Advertisement
kajacx

sort

Sep 17th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package test;
  6.  
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.Date;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author kajacx
  19.  */
  20. public class SortMe {
  21.    
  22.     private static final SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
  23.     private final ArrayList<Entry> entries;
  24.  
  25.     private SortMe() {
  26.         this.entries = new ArrayList<>();
  27.     }
  28.    
  29.     public static void main(String[] args) {
  30.         SortMe sm = new SortMe();
  31.         sm.addMany(20);
  32.         sm.print();
  33.        
  34.         sm.sort(new NameComparator());
  35.         sm.print();
  36.        
  37.         sm.sort(new DateComparator());
  38.         sm.print();
  39.        
  40.         sm.sort(new NumberComparator());
  41.         sm.print();
  42.     }
  43.    
  44.     private void print() {
  45.         for(Entry e: entries) {
  46.             System.out.println(e);
  47.         }
  48.         System.out.println();
  49.     }
  50.    
  51.     private void addMany(int count) {
  52.         for(int i = 0; i < count; i++) {
  53.             addOne();
  54.         }
  55.     }
  56.    
  57.     private void addOne() {
  58.         new Entry(Math.random()+"", ((int)(Math.random()*50) + 1970)+ "", (int) (Math.random()*50000));
  59.     }
  60.    
  61.     private void sort(Comparator c) {
  62.         Collections.sort(entries, c);
  63.     }
  64.    
  65.     private static class NumberComparator implements Comparator {
  66.  
  67.         @Override
  68.         public int compare(Object ob1, Object ob2) {
  69.             Entry o1 = (Entry) ob1;
  70.             Entry o2 = (Entry) ob2;
  71.            
  72.             int result = o1.number - o2.number;
  73.             //System.out.format("%s - %s = %s \n", o1.name, o2.name, result);
  74.             return result;
  75.         }
  76.        
  77.     }
  78.    
  79.     private static class DateComparator implements Comparator {
  80.  
  81.         @Override
  82.         public int compare(Object ob1, Object ob2) {
  83.             Entry o1 = (Entry) ob1;
  84.             Entry o2 = (Entry) ob2;
  85.            
  86.             Date d1, d2;
  87.             try {
  88.                 d1 = sdf.parse(o1.date);
  89.                 d2 = sdf.parse(o2.date);
  90.             } catch (ParseException ex) {
  91.                 Logger.getLogger(SortMe.class.getName()).log(Level.SEVERE, null, ex);
  92.                 return 0;
  93.             }
  94.            
  95.             int result = d1.compareTo(d2);
  96.             //System.out.format("%s - %s = %s \n", o1.name, o2.name, result);
  97.             return result;
  98.         }
  99.        
  100.     }
  101.    
  102.     private static class NameComparator implements Comparator {
  103.  
  104.         @Override
  105.         public int compare(Object ob1, Object ob2) {
  106.             Entry o1 = (Entry) ob1;
  107.             Entry o2 = (Entry) ob2;
  108.             int result = o1.name.compareTo(o2.name);
  109.             //System.out.format("%s - %s = %s \n", o1.name, o2.name, result);
  110.             return result;
  111.         }
  112.        
  113.     }
  114.    
  115.     private class Entry {
  116.         private final String name, date;
  117.         private final int number;
  118.  
  119.         private Entry(String name, String date, int number) {
  120.             this.name = name;
  121.             this.date = date;
  122.             this.number = number;
  123.             entries.add(this);
  124.         }
  125.  
  126.         @Override
  127.         public String toString() {
  128.             return "Entry{" + "name=" + name + ", date=" + date + ", number=" + number + '}';
  129.         }
  130.        
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement