你说的排序是什么意思?是按字符串长度吗?
要是按长度排序我有。
public class SortUtil
{
public SortUtil()
{
}
protected static void swap2( String[] x, int a, int b )
{
String t = x[ a ];
x[ a ] = x[ b ];
x[ b ] = t;
}
protected static void sortArrys( String[] a )
{
String temp;
for ( int i = 0; i < ( a.length / 2 ); i++ )
{
temp = a[ a.length - i - 1 ];
a[ a.length - i - 1 ] = a[ i ];
a[ i ] = temp;
}
return;
}
/**
* 对string数组按其中string长度由长到短排序
* @param String[]
*/
public static void sortByStrLength( String[] s )
{
// Insertion sort on smallest arrays
int[] is = new int[ s.length ];
int off = 0;
int len = is.length;
for ( int i = 0; i < is.length; i++ )
{
is[ i ] = s[ i ].getBytes().length;
}
if ( true )
{
for ( int i = off; i < len + off; i++ )
{
for ( int j = i; j > off && is[ j - 1 ] > is[ j ]; j-- )
{
swap2( s, j, j - 1 );
for ( int h = 0; h < is.length; h++ )
{
is[ h ] = s[ h ].getBytes().length;
}
}
}
sortArrys( s );
return;
}
}
public static void main( String[] args )
{
String[] s = {
"中", "bbbb", "cc", "d", "eeeee", "ff", "asdfda", "eef", "de",
"cc中国", "中国人", "卧室中国"
};
sortByStrLength( s );
for ( int k = 0; k < s.length; k++ )
{
System.out.println( s[ k ] );
}
}
}