posts - 431,  comments - 344,  trackbacks - 0
          需求:公司要將員工進行排序(不要說領導排在前面),我們的需求比較復雜。先進行姓排序,誰的姓拼音靠前,誰就排前面。然后對名字進行排序。恩.如果同名,女性排前頭。如果名字和性別都相同,年齡小的排前頭。
          代碼如下:

          Person.java

          package com.founder.common;

          public class Person {
           private String firstName;
           private String lastName;
           private boolean sex;
           private int age;

           public Person(String firstName, String lastName, boolean sex, int age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.sex = sex;
            this.age = age;
           }
           public int getAge() {
            return age;
           }
           public void setAge(int age) {
            this.age = age;
           }
           public String getFirstName() {
            return firstName;
           }
           public void setFirstName(String firstName) {
            this.firstName = firstName;
           }
           public String getLastName() {
            return lastName;
           }
           public void setLastName(String lastName) {
            this.lastName = lastName;
           }
           public boolean getSex() {
            return sex;
           }
           public void setSex(boolean sex) {
            this.sex = sex;
           }
           
           public String toString() {
            return firstName + " " + lastName + " " + (sex ? "Male" : "Female") + " " + age;
           }
          }

          Comparator.java

          package com.founder.common;

          public class Comparator{
           public static Java.util.Comparator getComparator() {
            return new Java.util.Comparator() {
             public int compare(Object o1, Object o2) {
              if (o1 instanceof String) {
               return compare((String) o1, (String) o2);
              } else if (o1 instanceof Integer) {
               return compare((Integer) o1, (Integer) o2);
              } else if (o1 instanceof Person) {
               return compare((Person) o1, (Person) o2);
              } else {
               System.err.println("未找到適合的比較器");
               return 1;
              }
             }
             
             public int compare(String o1, String o2) {
              int len1 = o1.length();
              int len2 = o2.length();
              int n = Math.min(len1, len2);
              char v1[] = o1.toCharArray();
              char v2[] = o2.toCharArray();
              int pos = 0;
              
              while (n-- != 0) {
               char c1 = v1[pos];
               char c2 = v2[pos];
               
               if (c1 != c2) {
                return c1 - c2;
               }
               pos++;
              }
              
              return len1 - len2;
             }
             
             public int compare(Integer o1, Integer o2) {  
                    int val1 = o1.intValue();  
                    int val2 = o2.intValue();  
                    return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));  
                }
             
             public int compare(boolean o1, boolean o2) {
              return (o1 == o2 ? 0 : (o1 == true ? 1: -1));
             }
             
             public int compare(Person o1, Person o2) {
              String firstname1 = o1.getFirstName();  
                    String firstname2 = o2.getFirstName();  
                    String lastname1 = o1.getLastName();  
                    String lastname2 = o2.getLastName();  
                    boolean sex1 = o1.getSex();  
                    boolean sex2 = o2.getSex();  
                    int age1 = o1.getAge();  
                    int age2 = o2.getAge();  
                    return (compare(firstname1, firstname2) == 0 ?
                      (compare(lastname1, lastname2) == 0 ? (compare(sex1, sex2) == 0 ? (compare(age1, age2) == 0 ? 0 :
                       compare(age1, age2)) :
                        compare(sex1, sex2)) :
                         compare(lastname1, lastname2)) :
                          compare(firstname1, firstname2));
             }
            };
           }
          }

          PersonTest.java

          package com.founder.common;

          public class PersonTest {

           public static void main(String[] args) {
            Person[] person = new Person[] {
              new Person("ouyang", "feng", true, 25),
              new Person("zhuang", "gw", false, 36),
              new Person("zhuang", "gw", true, 36),
              new Person("zhuang", "gw", true, 33),
              new Person("zhuang", "gw", false, 23),
              new Person("yang", "gw", true, 43)};
            for (int i = 0; i < person.length; i++) {
             System.out.println("before sort=" + person[i]);
            }

            java.util.Arrays.sort(person, Comparator.getComparator());

            System.out.println("*************************************");
            
            for (int i = 0; i < person.length; i++) {
             System.out.println("after sort=" + person[i]);
            }

           }

          }


          輸出結果為:
          before sort=ouyang feng Male 25
          before sort=zhuang gw Female 36
          before sort=zhuang gw Male 36
          before sort=zhuang gw Male 33
          before sort=zhuang gw Female 23
          before sort=yang gw Male 43
          *************************************
          after sort=ouyang feng Male 25
          after sort=yang gw Male 43
          after sort=zhuang gw Female 23
          after sort=zhuang gw Female 36
          after sort=zhuang gw Male 33
          after sort=zhuang gw Male 36

          posted on 2008-04-16 20:47 周銳 閱讀(2777) 評論(5)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 张北县| 凤山县| 云林县| 平遥县| 宣威市| 凤山市| 武宁县| 永平县| 潼关县| 左贡县| 图片| 大同县| 明溪县| 虹口区| 南通市| 永和县| 邢台县| 嵊泗县| 黔东| 仲巴县| 章丘市| 巢湖市| 宜兰市| 都匀市| 盐城市| 镇安县| 湘潭市| 静安区| 江川县| 定兴县| 沅江市| 夏河县| 蓬莱市| 威信县| 喀喇沁旗| 吉水县| 宜良县| 灵石县| 杭锦旗| 池州市| 肇庆市|