sort內(nèi)部按數(shù)字,大寫字母,小寫字母的優(yōu)先級(jí)排序,有圖有真相
public void testSort2(){
List<String>sb=new ArrayList<String>();
Random random=new Random();
//生成十條不重復(fù)的字符串(由數(shù)字和字母組成)
for(int i=0;i<10;i++){
StringBuffer str=new StringBuffer();
do{
//字符串長(zhǎng)度為10以內(nèi)的整數(shù),nextInt()范圍為[0,10),字符串長(zhǎng)度不想設(shè)置為空就加1
int size=1+random.nextInt(10);
//隨機(jī)生成字符串
for(int tempsize=0;tempsize<size;tempsize++){
int c;
do{
//隨機(jī)生成0到122的數(shù)字,0到9表示數(shù)字字符,65到90表示'A'到'Z'
//97到122表示'a'到'z'
c=(int)(Math.random()*123);
}
//當(dāng)生成的字符為'0'到'9'或'a'到'z'或'A'到'Z'時(shí)跳出
while(!((c>=0&&c<=9)||(c>='a'&&c<='z')||(c>='A'&&c<='Z')));
if(c>=0&&c<=9){
String s=Integer.toString(c);
str.append(s);
}
else{
char cr=(char)c;
String s=String.valueOf(cr);
str.append(s);
}
}
}while(sb.contains(str.toString()));
//如果list沒有該字符串就加入
sb.add(str.toString());
}
System.out.println("排序前的字符串list為:");
for (String string : sb) {
System.out.println(string);
}
System.out.println("排序后的字符串list為:");
Collections.sort(sb);
for (String string : sb) {
System.out.println(string);
}
}
2019-01-18
排序應(yīng)該是按照ASCII碼表上的順序進(jìn)行排列的,可以在百度上搜一下ASCII碼表,優(yōu)先級(jí)就出來了