这个题目搞得我头痛!请牛人来指教指教!
如下所示:
总分 年龄
620 22
649 43
700 34
580 18
700 25
700 21
649 21题目的要求是这样的: 首先对总分进行升序排序,如果总分相等则对总分相对应的年龄进行升序排序,其他的年龄不动!
如:总分为700的年龄有3个分别为34,25,21,则要对这三个年龄进行排序!其他的不变!要求用二维排序来做!坐等牛人出现...
如下所示:
总分 年龄
620 22
649 43
700 34
580 18
700 25
700 21
649 21题目的要求是这样的: 首先对总分进行升序排序,如果总分相等则对总分相对应的年龄进行升序排序,其他的年龄不动!
如:总分为700的年龄有3个分别为34,25,21,则要对这三个年龄进行排序!其他的不变!要求用二维排序来做!坐等牛人出现...
{
public static void sort(int num[][])
{
for (int i = 0; i < num.length; i++)
{
for (int j = i + 1; j < num.length; j++)
{
if (num[j][0] < num[i][0])
{
int temp = num[j][0];
num[j][0] = num[i][0];
num[i][0] = temp;
}
else if (num[j][0] == num[i][0]) {
if (num[j][1] < num[i][1])
{
int temp = num[j][1];
num[j][1] = num[i][1];
num[i][1] = temp;
}
}
}
}
}
public static void main(String[] args)
{
int num[][] = {{600,22},{649,43},{700,34},{580,18},{700,25},{700,21},{649,21},{700,1}};
sort(num);
for (int i = 0; i < num.length; i++)
{
System.out.println(num[i][0] + " " + num[i][1]);
}
}
}
方法:1二维数组 自己写个sort 函数方法:2插入数据库select 总分 ,年龄 from 表 order by 总分 ,年龄方法:3使用API + Comparator 接口
/**
* @author troy(J2EE)
* @version 1.0
*/
import java.util.*;
public class Test{
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception{
String[] ayy={"620 22","649 43","700 34","580 18","700 25","700 21","649 21"};
Arrays.sort(ayy,new howToSort());
for (int i=0;i<ayy.length ;i++ ){
System.out.println(ayy[i]);
}
}
}
//排序
class howToSort implements Comparator {
public final int compare(Object firstObj, Object secondObj){
int a=Integer.valueOf(((String)firstObj).substring(0,3)) - Integer.valueOf(((String)secondObj).substring(0,3));
int b=Integer.valueOf(((String)firstObj).substring(4)) - Integer.valueOf(((String)secondObj).substring(4));
return a==0?b:a;
}
}
public class Demo {
private double point;
private int age; public Demo(double point,int age){
this.point=point;
this.age=age;
}
public double getPoint() {
return point;
} public void setPoint(double point) {
this.point = point;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] arg) {
Demo[] demo=new Demo[7];
demo[0]=new Demo(620,20);
demo[1]=new Demo(649,43);
demo[2]=new Demo(700,34);
demo[3]=new Demo(580,18);
demo[4]=new Demo(700,25);
demo[5]=new Demo(700,21);
demo[6]=new Demo(649,21);
Arrays.sort(demo,new Comparator<Demo>(){ public int compare(Demo o1, Demo o2) {
if(o1.getPoint()<o2.getPoint())
return 1;
else if(o1.getPoint()==o2.getPoint()){
if(o1.getAge()<o2.getAge()){
return 1;
}else if(o1.getAge()==o2.getAge()){
return 0;
}else
return -1;
}else return -1;
}
});
for(Demo d:demo){
System.out.println(d.getPoint()+" "+d.getAge());
} }
}
Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) {
int a = o1[0] - o2[0];
if (a != 0) {
return a;
}
return o1[1] - o2[1];
}
});
}
int[] tmp = null;
for(int i = 0 ; i < sAndAge.length-1 ; i++){
for(int j = i ; j < sAndAge.length ; j++ ){
if(sAndAge[i][0] > sAndAge[j][0]){
tmp = sAndAge[i];
sAndAge[i] = sAndAge[j];
sAndAge[j] = tmp;
}else if(sAndAge[i][0] == sAndAge[j][0] && sAndAge[i][1] > sAndAge[j][1]){
tmp = sAndAge[i];
sAndAge[i] = sAndAge[j];
sAndAge[j] = tmp;
}
}
}
for(int i = 0 ; i < sAndAge.length ;i++){
System.out.println(Arrays.toString(sAndAge[i]));
}
int[] tmp = null;
for(int i = 0 ; i < sAndAge.length-1 ; i++){
for(int j = i ; j < sAndAge.length ; j++ ){
if(sAndAge[i][0] > sAndAge[j][0]){
tmp = sAndAge[i];
sAndAge[i] = sAndAge[j];
sAndAge[j] = tmp;
}else if(sAndAge[i][0] == sAndAge[j][0] && sAndAge[i][1] > sAndAge[j][1]){
tmp = sAndAge[i];
sAndAge[i] = sAndAge[j];
sAndAge[j] = tmp;
}
}
}
for(int i = 0 ; i < sAndAge.length ;i++){
System.out.println(Arrays.toString(sAndAge[i]));
}
{
public static void main(String[] args)
{ int[][]arr={{620,649,700,580,700,700,649},{22,43,34,18,25,21,21}};
arraySort(arr);
arraySort2(arr);
printArray(arr);
}
public static void arraySort(int[][] arr){
for(int x=1;x<arr[0].length;x++){
for(int y=0;y<arr[0].length-x;y++){
int temp;
if(arr[0][y]>arr[0][y+1]){
temp=arr[0][y];
arr[0][y]=arr[0][y+1];
arr[0][y+1]=temp; temp=arr[1][y];
arr[1][y]=arr[1][y+1];
arr[1][y+1]=temp;
}
}
}
}
public static void arraySort2(int[][]arr){
for(int x=1;x<arr[0].length;x++){
for(int y=0;y<arr[0].length-x;y++){
if(arr[0][y]==arr[0][y+1]){
if(arr[1][y]>arr[1][y+1]){
int temp;
temp=arr[1][y];
arr[1][y]=arr[1][y+1];
arr[1][y+1]=temp;
}
}
}
}
}
public static void printArray(int[][]arr){
for(int x=0;x<arr[0].length;x++){
System.out.print(arr[0][x]+"\t"+arr[1][x]);
System.out.println();
}
}
}
class Test
{
public static void main(String[] args)
{ int[][]arr={{620,649,700,580,700,700,649},{22,43,34,18,25,21,21}};
arraySort(arr);
arraySort2(arr);
printArray(arr);
}
public static void arraySort(int[][] arr){
for(int x=1;x<arr[0].length;x++){
for(int y=0;y<arr[0].length-x;y++){
int temp;
if(arr[0][y]>arr[0][y+1]){
temp=arr[0][y];
arr[0][y]=arr[0][y+1];
arr[0][y+1]=temp; temp=arr[1][y];
arr[1][y]=arr[1][y+1];
arr[1][y+1]=temp;
}
}
}
}
public static void arraySort2(int[][]arr){
for(int x=1;x<arr[0].length;x++){
for(int y=0;y<arr[0].length-x;y++){
if(arr[0][y]==arr[0][y+1]){
if(arr[1][y]>arr[1][y+1]){
int temp;
temp=arr[1][y];
arr[1][y]=arr[1][y+1];
arr[1][y+1]=temp;
}
}
}
}
}
public static void printArray(int[][]arr){
for(int x=0;x<arr[0].length;x++){
System.out.print(arr[0][x]+"\t"+arr[1][x]);
System.out.println();
}
}
}
public static void sort(int num[][])
{
for (int i = 0; i < num.length; i++)
{
for (int j = i + 1; j < num.length; j++)
{
if (num[j][0] < num[i][0])
{
int temp = num[j][0];
num[j][0] = num[i][0];
num[i][0] = temp;
temp = num[j][1];
num[j][1] = num[i][1];
num[i][1] = temp;
}
else if (num[j][0] == num[i][0]) {
if (num[j][1] < num[i][1])
{
int temp = num[j][1];
num[j][1] = num[i][1];
num[i][1] = temp;
temp = num[j][0];
num[j][0] = num[i][0];
num[i][0] = temp;
}
}
}
}
}