你说的排序是什么意思?是按字符串长度吗?
要是按长度排序我有。
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 ] );
}
}
}
要是按长度排序我有。
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 ] );
}
}
}
解决方案 »
- Invalid DN Syntax (34) Invalid DN Syntax
- 图形处理问题请教,INT_8888_ARGB to USHORT_4444_ARGB.
- 在windows 2003 server下整合apache2.2和tomcat5.5,有点难度,都来看看
- jreechart的折线图中,如果线太靠上,点上显示的数值被盖住了,显示不出来,哪位高手帮忙解决一下
- 请教一个JSP问题,急!!
- 谁能告诉我这是什么意思?
- 录入界面里面有 照片,请大家帮忙,怎么作?
- why????????关于application中画图的问题!!!!!!!高分求解
- itext中那个方法可以把文字版的pdf转换成图片版的pdf
- 如何使用字节流代替FileItem上传zip等文件
- 这样的日期格式"2002-09-08",能否被Calendar类直接使用,从而得到这个特定日期的一些信息??
- 用388模拟器调试程序时出现** loadImage MediaTracker error 怎么解决
如上面的例子,
结果要为北京------(beijing)
亚运会-----(yayunhui)
中国--------(zhongguo)
我没有做优化,重复次数较多。你可以改进改进。
public class Test {
public static class PYComparator implements Comparator{
public int compare(Object o1,Object o2) {
try {
byte[] buf1 = ((String)o1).getBytes("GB2312");
byte[] buf2 = ((String)o2).getBytes("GB2312");
int size = Math.min(buf1.length,buf2.length);
for(int i = 0; i < size; i++) {
if(buf1[i] < buf2[i])
return -1;
else if(buf1[i] > buf2[i])
return 1;
}
return buf1.length - buf2.length;
}
catch (UnsupportedEncodingException ex) {
return 0;
}
}
}
public static void main(String[] args) throws Exception{
String[] str = {"北京","中国","亚运会"};
Arrays.sort(str,new PYComparator());
for(int i = 0 ; i < str.length; i++)
System.out.println(str[i]);
}
}
我是java初学,谢谢了。
结帖给分。呵呵