给定一个字符串数组{"nba","abc","cba","zz","qq","haha"},请按照字典顺序进行从小到大的排序。...

思路:冒泡排序,compareTo()或compareToIgnoreCase()比较字符串

package com.jn.test;

/*
 * 给定一个字符串数组{"nba","abc","cba","zz","qq","haha"},
 * 请按照字典顺序进行从小到大的排序。
 */
public class StringCompositor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] arr = {"nba","abc","cba","zz","qq","haha","ac"};
		for(int i=0;i0){
					String temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
		for (int i=0;i

注:

compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)

1、如果字符串相等返回值0

2、如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值)(负值代表:前字符串的值小于后字符串,正值代表:前字符串大于后字符串)

3、如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度. 

compareToIgnoreCase()方法是不区分大小写,返回值是int,比较方式与compareTo()相同

你可能感兴趣的:(给定一个字符串数组{"nba","abc","cba","zz","qq","haha"},请按照字典顺序进行从小到大的排序。...)