//JAVA原装的String比较方法
/**
* Compares two strings lexicographically.
* The comparison is based on the Unicode value of each character in
* the strings. The character sequence represented by this
* String
object is compared lexicographically to the
* character sequence represented by the argument string. The result is
* a negative integer if this String
object
* lexicographically precedes the argument string. The result is a
* positive integer if this String
object lexicographically
* follows the argument string. The result is zero if the strings
* are equal; compareTo
returns 0
exactly when
* the {@link #equals(Object)} method would return true
.
*
* This is the definition of lexicographic ordering. If two strings are
* different, then either they have different characters at some index
* that is a valid index for both strings, or their lengths are different,
* or both. If they have different characters at one or more index
* positions, let k be the smallest such index; then the string
* whose character at position k has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, compareTo
returns the
* difference of the two character values at position k
in
* the two string -- that is, the value:
*
* this.charAt(k)-anotherString.charAt(k)
*
compareTo
returns the difference of the lengths of the
* this.length()-anotherString.length()
*
String
to be compared.0
if the argument string is equal to0
if this string0
if this string is必须用compareTo()吗
不是必须的话这样就行
import java.util.*;
public class TextString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
char[] ch = s.toCharArray();
Arrays.sort(ch);
System.out.println(ch);
}
}
public class MyClass {
public void Sort(String[] data)
{
int l=data.length;
String tmp;
for(int i=0;i
for(int j=i+1;j
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
System.out.println(data[i]);
}
}
}
public static void getSort(String str){
char []chs = str.toCharArray();
Arrays.sort(chs);
System.out.println(chs);
}
这个都能用那还怕什么啊?直接用一个char【】数组就可以了啊,冒泡法排序啊,最简单的。