根据数字的首字母大小排序

来源:互联网 发布:软件绿化工具 编辑:程序博客网 时间:2024/06/07 11:45
import java.io.*;
import java.util.*;

//用户输入几个数据,然后根据这些数据的首个数字的大小排序

public class Problem1 {

    public static void main(String[] args) throws Exception{
       
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int num=Integer.parseInt(br.readLine());        //输入的个数 
       
        String str[]=new String[num];
        for(int i=0;i<num;i++){
           
            str[i]=br.readLine();
           
        }
       
        sort(str);

    }
   
    static void sort(String str[]){            //根据首个数字的大小排序
       
        String my[]=new String[str.length];
        int a[]=new int[str.length];
       
        for(int i=0;i<str.length;i++){   
       
            my[i]=str[i].substring(0, 1);
            a[i]=Integer.parseInt(my[i]);
           
        }
       
        bubbleSort(a,str);
       
    }
   
    static void bubbleSort(int []a,String str[]){ //排序算法
       
        for(int i=0;i<a.length;i++){
            boolean flag=true;
            for(int j=1;j<a.length-i;j++)
            if(a[j-1]>a[j]){
                int temp=a[j-1];
                a[j-1]=a[j];
                a[j]=temp;
               
                String temp2=str[j-1];
                str[j-1]=str[j];
                str[j]=temp2;
                flag=false;
               
            }
            if(flag)break;
           

        }
        for(int i=0;i<a.length;i++){         //输出原数组
           
            //System.out.println(a[i]);
            System.out.println(str[i]);
           
        }
   
    }
}