1. 使用Comparable排序- import java.util.*;
- public class ComparableTest{
- public static void main(String[] args ){
- ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
- populateList(dvdList); // adds the file data to the ArrayList
- System.out.println("Before Sorting:");
- System.out.println(dvdList);
-
- Collections.sort(dvdList);
- System.out.println("After Sorting:");
- System.out.println(dvdList);
- }
-
- class DVDInfo implements Comparable<DVDInfo> {
- String title;
- String genre;
- String leadActor;
- DVDInfo(String t, String g, String a) {
- title = t; genre = g; leadActor = a;
- }
- public String toString() {
- return title + " " + genre + " " + leadActor + "/n";
- }
- public int compareTo(DVDInfo d) {
- return title.compareTo(d.getTitle());
- }
- public String getTitle() {
- return title;
- }
- // other getters and setters
- }
-
- public static void populateList(ArrayList<DVDInfo> dvdList){
- //Call inner class from a static context. use new OuterClass.new InnerClass()
- DVDInfo di1 = new ComparableTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
- DVDInfo di2 = new ComparableTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
- DVDInfo di3 = new ComparableTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
- dvdList.add(di1);
- dvdList.add(di2);
- dvdList.add(di3);
- }
- }
- /*
- Result:
- Before Sorting:
- [Matrix Science Fiction Keanu Reeves
- , Forrest Gump Comedy-drama Tom Hanks
- , Mission Impossible Action Tom Cruise
- ]
- After Sorting:
- [Forrest Gump Comedy-drama Tom Hanks
- , Matrix Science Fiction Keanu Reeves
- , Mission Impossible Action Tom Cruise
- ]
- */
复制代码 2. 使用Comparator排序- import java.util.*;
- public class ComparatorTest{
- public static void main(String[] args ){
- ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
- populateList(dvdList); // adds the file data to the ArrayList
- System.out.println("Before Sorting:");
- System.out.println(dvdList);
-
- TitleComparator tc = new ComparatorTest().new TitleComparator();
- Collections.sort(dvdList, tc);
- System.out.println("After Sorting:");
- System.out.println(dvdList);
- }
-
- class TitleComparator implements Comparator<DVDInfo>{
- public int compare(DVDInfo one, DVDInfo two){
- return one.getTitle().compareTo(two.getTitle());
- }
- }
-
- //No need to implement Comparable
- class DVDInfo {
- String title;
- String genre;
- String leadActor;
- DVDInfo(String t, String g, String a) {
- title = t; genre = g; leadActor = a;
- }
- public String toString() {
- return title + " " + genre + " " + leadActor + "/n";
- }
-
- public String getTitle() {
- return title;
- }
- // other getters and setters
- }
-
- public static void populateList(ArrayList<DVDInfo> dvdList){
- //Call inner class from a static context. use new OuterClass.new InnerClass()
- DVDInfo di1 = new ComparatorTest().new DVDInfo("Matrix", "Science Fiction", "Keanu Reeves");
- DVDInfo di2 = new ComparatorTest().new DVDInfo("Forrest Gump", "Comedy-drama", "Tom Hanks");
- DVDInfo di3 = new ComparatorTest().new DVDInfo("Mission Impossible", "Action", "Tom Cruise");
- dvdList.add(di1);
- dvdList.add(di2);
- dvdList.add(di3);
- }
- }
- /*
- Result:
- Before Sorting:
- [Matrix Science Fiction Keanu Reeves
- , Forrest Gump Comedy-drama Tom Hanks
- , Mission Impossible Action Tom Cruise
- ]
- After Sorting:
- [Forrest Gump Comedy-drama Tom Hanks
- , Matrix Science Fiction Keanu Reeves
- , Mission Impossible Action Tom Cruise
- ]
- */
复制代码 出处:http://blog.csdn.net/tohmin/article/details/6142611 |
|