1
2
3
4
5 package checkex;
6
7 import java.text.DateFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17
18
19
20 @author
21
22 public class comparesortandorder {
23 public static void main(String[] args) {
24 List<Person> persons = new java.util.ArrayList<Person>();
25 persons.add(new Person("a", "def5", -10, "10/10/12"));
26 persons.add(new Person("a", "def3", 70, "01/01/13"));
27 persons.add(new Person("b", "def22", 32, "01/01/2013"));
28 persons.add(new Person("b", "def1", 65, "01/01/13"));
29 persons.add(new Person("a", "def4", -10, "10/10/12"));
30 persons.add(new Person("a", "def4", -10, "10/10/19"));
31 persons.add(new Person("c", "def22", 10, "01/01/2013"));
32 persons.add(new Person("c", "def21", 10, "01/01/2013"));
33
34 System.out.println(persons);
35 Collections.sort(persons, new Comparator<Person>() {
36
37 @Override
38 public int compare(Person t, Person t1) {
39
40 if(0 == t.getAge() || 0== t1.getAge() ){
41 throw new NullPointerException();
42 }else{
43 int nameComparisonResult = t.getAge()-(t1.getAge());
44 if(0 == nameComparisonResult){
45 return t.getLname().compareTo(t1.getLname());
46 }else{
47 return nameComparisonResult;
48 }
49 }
50 }
51 });
52 System.out.println(persons);
53
54
55 List<Person> a1 = new ArrayList<Person>();
56 for (int i = 0; i < persons.size(); i++) {
57 if((persons.get(i).getFname() == "a") || (persons.get(i).getFname() == "d") ){
58 a1.add(new Person(persons.get(i).getFname(), persons.get(i).getLname(),persons.get(i).getAge(), persons.get(i).getDateval()));
59 }
60 }
61 System.out.println(a1);
62
63 Collections.sort(a1, new Comparator<Person>() {
64
65 @Override
66 public int compare(Person o1, Person o2) {
67
68
69 if (o1.getDateval() == null || o2.getDateval() == null)
70 return 0;
71 try {
72 return converttodate(o1.getDateval()).compareTo(converttodate(o2.getDateval()));
73 } catch (ParseException ex) {
74 Logger.getLogger(comparesortandorder.class.getName()).log(Level.SEVERE, null, ex);
75 }
76 return 0;
77 }
78
79 });
80 System.out.println(a1);
81
82
83 }
84 public static Date converttodate(String dateval) throws ParseException {
85
86 DateFormat formatter ;
87 Date date ;
88 formatter = new SimpleDateFormat("MM/dd/yy");
89 date = formatter.parse(dateval);
90 return date;
91
92
93 }
94 }
95
1
2
3
4
5 package checkex;
6
7
8
9 @author
10
11 public class Person {
12 String fname;
13 String lname;
14 String dateval;
15 int age;
16
17 public Person() {
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 public void setAge(int age) {
25 this.age = age;
26 }
27 public String getDateval() {
28 return dateval;
29 }
30
31 public void setDateval(String dateval) {
32 this.dateval = dateval;
33 }
34
35 public String getFname() {
36 return fname;
37 }
38
39 public void setFname(String fname) {
40 this.fname = fname;
41 }
42
43 public String getLname() {
44 return lname;
45 }
46
47 public void setLname(String lname) {
48 this.lname = lname;
49 }
50
51 public Person(String fname, String lname, int age, String dateval) {
52 this.fname = fname;
53 this.lname = lname;
54 this.age = age;
55 this.dateval = dateval;
56 }
57
58 @Override
59 public String toString() {
60 return fname + "," + lname + "," + age+","+dateval;
61 }
62 }
63