1. import java.util.Arrays;   
  2. import java.util.Comparator;   
  3.   
  4. /**  
  5.  * 微软面试题-按照字母排序.<br>  
  6.  * 小写字母在大写后面,字母B/b在字母A/a后面<br>  
  7.  *   
  8.  * @author 老紫竹 JAVA世纪网(java2000.net)  
  9.  *   
  10.  */  
  11. class T {   
  12.   private static final int MASK = 0xFFDF// 用来把小写变成大写   
  13.   
  14.   public static int compare(String o1, String o2) {   
  15.     int length1 = o1.length();   
  16.     int length2 = o2.length();   
  17.     int length = length1 > length2 ? length2 : length1;   
  18.     int c1, c2;   
  19.     int d1, d2;   
  20.     for (int i = 0; i < length; i++) {   
  21.       c1 = o1.charAt(i);   
  22.       c2 = o2.charAt(i);   
  23.       d1 = c1 & MASK;   
  24.       d2 = c2 & MASK;   
  25.       if (d1 > d2) {   
  26.         return 1;   
  27.       } else if (d1 < d2) {   
  28.         return -1;   
  29.       } else {   
  30.         if (c1 > c2) {   
  31.           return 1;   
  32.         } else if (c1 < c2) {   
  33.           return -1;   
  34.         }   
  35.       }   
  36.     }   
  37.     if (length1 > length2) {   
  38.       return 1;   
  39.     } else if (length1 < length2) {   
  40.       return -1;   
  41.     }   
  42.     return 0;   
  43.   }   
  44.   
  45.   public static void sortByPOPO(String[] args) {   
  46.     String tmp;   
  47.     for (int i = 0; i < args.length; i++) {   
  48.       for (int j = i + 1; j < args.length; j++) {   
  49.         if (compare(args[i], args[j]) > 0) {   
  50.           tmp = args[i];   
  51.           args[i] = args[j];   
  52.           args[j] = tmp;   
  53.         }   
  54.       }   
  55.     }   
  56.     // [Ad, aC, Bc, During, day, Hello, little, X man]   
  57.     System.out.println(Arrays.toString(args));   
  58.   }   
  59.   
  60.   public static void main(String[] args) {   
  61.     String[] strs = { "Bc""Ad""aC""Hello""X man""little""During",   
  62.         "day" };   
  63.   
  64.     sortByPOPO(strs);   
  65.   
  66.   }   
  67. }  
import java.util.Arrays;
import java.util.Comparator;

/**
 * 微软面试题-按照字母排序.<br>
 * 小写字母在大写后面,字母B/b在字母A/a后面<br>
 * 
 * @author 老紫竹 JAVA世纪网(java2000.net)
 * 
 */
class T {
  private static final int MASK = 0xFFDF; // 用来把小写变成大写

  public static int compare(String o1, String o2) {
    int length1 = o1.length();
    int length2 = o2.length();
    int length = length1 > length2 ? length2 : length1;
    int c1, c2;
    int d1, d2;
    for (int i = 0; i < length; i++) {
      c1 = o1.charAt(i);
      c2 = o2.charAt(i);
      d1 = c1 & MASK;
      d2 = c2 & MASK;
      if (d1 > d2) {
        return 1;
      } else if (d1 < d2) {
        return -1;
      } else {
        if (c1 > c2) {
          return 1;
        } else if (c1 < c2) {
          return -1;
        }
      }
    }
    if (length1 > length2) {
      return 1;
    } else if (length1 < length2) {
      return -1;
    }
    return 0;
  }

  public static void sortByPOPO(String[] args) {
    String tmp;
    for (int i = 0; i < args.length; i++) {
      for (int j = i + 1; j < args.length; j++) {
        if (compare(args[i], args[j]) > 0) {
          tmp = args[i];
          args[i] = args[j];
          args[j] = tmp;
        }
      }
    }
    // [Ad, aC, Bc, During, day, Hello, little, X man]
    System.out.println(Arrays.toString(args));
  }

  public static void main(String[] args) {
    String[] strs = { "Bc", "Ad", "aC", "Hello", "X man", "little", "During",
        "day" };

    sortByPOPO(strs);

  }
}

相关文章:

  • 2021-12-12
  • 2022-12-23
  • 2021-12-15
  • 2021-12-15
  • 2021-11-18
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
猜你喜欢
  • 2021-08-01
  • 2021-07-08
  • 2021-12-15
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案