㈠ java中怎么对一串字符进行排序!
importjava.util.Arrays;
publicclassStringSort{
publicstaticvoidmain(String[]args){
Stringstring="togoodtoonostringandnow";
StringsortString=sortChar(string);
System.out.println(sortString);
}
privatestaticStringsortChar(Stringstr){
//1.将字符串转化成数组
char[]chs=stringToArray(str);
//2.对数组进行排序
sort(chs);
//3.将数组转成字符串
returntoString(chs);
}
privatestaticStringtoString(char[]chs){
returnnewString(chs);
}
//对字符数组进行升序排序
privatestaticvoidsort(char[]chs){
Arrays.sort(chs);
}
//将字符串转化成为数组
privatestaticchar[]stringToArray(Stringstring){
returnstring.toCharArray();
}
}
㈡ java 字符串排序
//这个绝对满足您的要求
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String ss[]={"ab","wang","hi","a","abff"};
MyString mySs[]=new MyString[ss.length];//创建自定义排序的数组
for (int i = 0; i < ss.length; i++) {
mySs[i]=new MyString(ss[i]);
}
Arrays.sort(mySs);//排序
for (int i = 0; i < mySs.length; i++) {
System.out.println(mySs[i].s);
}
}
}
class MyString implements Comparable<MyString>{
public String s;//包装String
public MyString(String s) {
this.s = s;
}
@Override
public int compareTo(MyString o) {
if(o==null||o.s==null) return 1;
if(s.length()>o.s.length()) return 1;
else if(s.length()<o.s.length()) return -1;
return s.compareTo(o.s);
}
}
㈢ 编写一个java程序对一个字符串数组进行排序
importjava.util.Arrays;
publicclassStringDateSort{
publicstaticvoidmain(String[]args){
StringnumString="2310-8037108";
StringsortStr=sortNumberString(numString);
System.out.println("排序的结果是"+sortStr);
}
(StringnumString){
//1.将给定的字符串分解成多个数字格式字符串
String[]numStrings=toStringArray(numString);
//2.将字符串数组转成int数组
int[]nums=toIntArray(numStrings);
//3.对数组排序
sort(nums);
//4.将int数组转成字符串
returntoString(nums);
}
privatestaticStringtoString(int[]nums){
StringBufferstringBuffer=newStringBuffer();
for(inti=0;i<nums.length;i++){
if(i!=nums.length-1){
stringBuffer.append(nums[i]+"<");
}else{
stringBuffer.append(nums[i]);
}
}
returnstringBuffer.toString();
}
privatestaticvoidsort(int[]nums){
Arrays.sort(nums);
}
privatestaticint[]toIntArray(String[]numStrings){
int[]nums=newint[numStrings.length];
for(inti=0;i<numStrings.length;i++){
Strings=numStrings[i];
nums[i]=Integer.parseInt(s);
}
returnnums;
}
privatestaticString[]toStringArray(StringnumString){
String[]numberStr=numString.split("");
for(inti=0;i<numberStr.length;i++){
System.out.println("抽出数字"+numberStr[i]);
}
returnnumberStr;
}
}