有一个班级集合 属性为班级人数classnum和班级名字classname
对其排序,按人数排,人数相同在按班级名字排,请问怎么排序啊?要用什么方法哦 ?
对其排序,按人数排,人数相同在按班级名字排,请问怎么排序啊?要用什么方法哦 ?
解决方案 »
- 如何让程序一直运行在前台
- Java、C++的发展方向
- 可以教下吗谢谢
- 关于java mysql jdbc中中文的问题,我在csdn中找过没找到答案,高人指点!
- 100分问个webservice配置问题.
- 请问java里面有什么类可以直接实现windows资源管理器的功能
- 不传递参数有没有办法取得调用自己的类的名称
- JTree问题,当我修改JTree节点上的字符串时,当修改后的长度比原来的长,就会在节点上显示...我该怎么样才能显示完整?
- 求教:java小程序用eclipse可以编译运行,用命令行编译通过但不能运行
- 如何用JAVA 删除本地硬盘上的一个目录下的文件?
- java 日文问题
- JAVA如何使用SQL语句??
我在网上搜了一个例子,改写了一下,供你参考
public class BubbleSort
{
/** *对数组obj中的元素以冒泡排序算法进行排序 */
public void sort(int[] obj)
{ if (obj == null) { throw new NullPointerException("The argument can not be null!");
}
int tmp;
for (int i = 0 ;i < obj.length ;i++ )
{ //切记,每次都要从第一个开始比。最后的不用再比。
for (int j = 0 ;j < obj.length - i - 1 ;j++ )
{ //对邻接的元素进行比较,如果后面的小,就交换
if (obj[j]-(obj[j + 1]) > 0)
{ tmp = obj[j];
obj[j] = obj[j + 1];
obj[j + 1] = tmp;
} }
} }
public static void main(String[] args){
BubbleSort bs=new BubbleSort();
int[] i={21,12,3,34,0,3,8,9};
bs.sort(i);
for(int j=0;j<i.length;j++){
System.out.println(i[j]);
}
}
}
public void sort(String[] obj)
{ if (obj == null) { throw new NullPointerException("The argument can not be null!");
}
int[] math=new int[obj.length];
String[] str=new String [obj.length];
for(int i=0;i<obj.length;i++){
int leftbrace=obj[i].indexOf('[');
str[i]=obj[i].substring(0,leftbrace);
String port=obj[i].substring(leftbrace+1,obj[i].length()-1);
math[i]=Integer.parseInt(port); }
int tmp;
String s;
for (int i = 0 ;i < math.length ;i++ )
{
for (int j = i+1 ;j < math.length ;j++ )
{
if (math[i]-math[j] > 0)
{ tmp = math[i];
math[i] = math[j];
math[j] = tmp;
s = str[i];
str[i] = str[j];
str[j] = s;
} }
}
for(int i=0;i<math.length;i++){
if(math[i]==math[i+1]){
if(str[i].compareTo(str[i+1])>0){
tmp = math[i];
math[i] = math[i+1];
math[i+1] = tmp;
s = str[i];
str[i] = str[i+1];
str[i+1] =s;
}
}
System.out.println(str[i]+math[i]);
}
System.out.println(str[math.length-1]+math[math.length-1]);
}
public static void main(String[] args){
try{
Count bs=new Count();
String[] bj={"计算机一班[43]","计算机二班[46]","计算机三班[43]","计算机四班[50]"};
bs.sort(bj);
}
catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}