java 数字,字符串数组比较大小

java字符串数组进行大小排序

若是将两个字符串直接比较大小,会包:The operator > is undefined for the argument type(s) java.lang.String, java.lang.String的错误。

字符串比较大小可以用字符串长度或者是比较字符串内字符的ASCII码值,前者太简单,就不进行讲述记录。

字符串用ASCII码比较大小,规则是:

1、比较首字母的ASCII码大小

2、若是前面的字母相同,则比较之后的字母的ASCII码值

3、若是一个字符串从首字母开始包含另一个字符串,则认为字符串长度较长的大;例 :abc > ab

package day0504_冒泡排序;

import java.util.Arrays;

public class test_排序 {

    public static void main(String[] args) {
        String[] a={"Now","is","the","time","for","all","good","men",
                "to","come","to","the","aid","of","their","country"};
        //System.out.println(a.length);
        // 调用方法,对数组a进行排序
         
                //sort(a);
              getUrlParam(a);
                System.out.println("----------------");
                System.out.println(Arrays.toString(a));
                System.out.println("----------------");
        //对a数组排序
                Arrays.sort(a);
                System.out.println(Arrays.toString(a));
                
    }
     /*  对字符串数组进行排序 */
    public static String[] getUrlParam(String[] keys){
         
         for (int i = 0; i < keys.length - 1; i++) {
             for (int j = 0; j < keys.length - i -1; j++) {
                 String pre = keys[j];
                String next = keys[j + 1];
                 if(isMoreThan(pre, next)){
                    String temp = pre;
                    keys[j] = next;
                    keys[j+1] = temp;
                 }
            }
         }
        return keys;
    }
    
    /*比较两个字符串的大小,按字母的ASCII码比较*/
     private static boolean isMoreThan(String pre, String next){
         if(null == pre || null == next || "".equals(pre) || "".equals(next)){
             System.out.println("字符串比较数据不能为空!");
             return false;
         }
         
        char[] c_pre = pre.toCharArray();
        char[] c_next = next.toCharArray();
         
         int minSize = Math.min(c_pre.length, c_next.length);
        
         for (int i = 0; i < minSize; i++) {
             if((int)c_pre[i] > (int)c_next[i]){
                 return true;
             }else if((int)c_pre[i] < (int)c_next[i]){
                 return false;
             }
         }
         if(c_pre.length > c_next.length){
             return true;
         }
         
         return false;
     }
     
     /*比较数字排序*/
     private static void sort(int[] a) {
         /*s.length-1 下标
          *     j
          * [36,2,4,37,85,46,10]
          *   i
          * 
          * */
         for (int i = 0; i < a.length; i++) {
             boolean flag = false; // 没有交换
             // 内层j循环,把较小值向前推,
             // 把最小值,推到i位置
             // j循环,从末尾到>i,递减
             for (int j = a.length-1; j > i ; j--) {
                 // j和j-1位置,较小值向前交换
                 if (a[j] < a[j - 1]) {
                     int t = a[j];
                     a[j] = a[j - 1];
                     a[j - 1] = t;
                     flag = true;// 表示交换了数据
                 }
                 
             }
             // j循环结束
             // 如果flag是false,j执行过程中没有交换
             if (!flag) {
                 break;
             }
                //System.out.println(Arrays.toString(a));
             }
     }

}

猜你喜欢

转载自www.cnblogs.com/kkxwze/p/12514172.html