posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2008年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 865031
          • 排名 - 44

          最新評論

          閱讀排行榜

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

          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 周銳 閱讀(2784) 評論(5)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 井研县| 道孚县| 盘锦市| 丘北县| 旬邑县| 瓦房店市| 阿拉善左旗| 天祝| 苏尼特右旗| 郯城县| 枣庄市| 无为县| 汉寿县| 南丰县| 海丰县| 沙雅县| 略阳县| 巫溪县| 济南市| 乌兰浩特市| 安吉县| 承德县| 沐川县| 赤壁市| 铁力市| 会泽县| 乌拉特中旗| 信丰县| 台中县| 上犹县| 江安县| 佛学| 会昌县| 神木县| 法库县| 青阳县| 伊川县| 太康县| 梓潼县| 揭阳市| 武城县|