Java 实现二维数组按指定列经行排序
2014-04-13 22:34
288 查看
[java]
view plaincopyprint?
import java.util.*;
public static void sortIntArray(int[][] arObjects, final int[] arOrders)
{
Arrays.sort(arObjects, new Comparator<Object>()
{
public int compare(Object oObjectA, Object oObjectB)
{
int[] arTempOne = (int[])oObjectA;
int[] arTempTwo = (int[])oObjectB;
for (int i = 0; i < arOrders.length; i++)
{
int k = arOrders[i];
if (arTempOne[k] > arTempTwo[k])
{
return 1;
}
else if (arTempOne[k] < arTempTwo[k])
{
return -1;
}
else
{
continue;
}
}
return 0;
}
});
}
调用方法是:
[java]
view plaincopyprint?
public static void main(String[] args)
{
int array[][] = new int[][] {
{ 12, 34, 68, 32, 9, 12, 545 },
{ 34, 72, 82, 57, 56, 0, 213 },
{ 12, 34, 68, 32, 21, 945, 23 },
{ 91, 10, 3, 2354, 73, 34, 18 },
{ 12, 83, 189, 26, 27, 98, 33 },
{ 47, 23, 889, 24, 899, 23, 657 },
{ 12, 34, 68, 343, 878, 235, 768 },
{ 12, 34, 98, 56, 78, 12, 546 },
{ 26, 78, 2365, 78, 34, 256, 873 } };
sortIntArray(array, new int[] { 0, 1 }); // 先根据第一列比较,若相同则再比较第二列
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
System.out.print(array[i][j]);
System.out.print("\t");
}
System.out.println();
}
}
注:本文转载自:http://blog.sina.com.cn/s/blog_5a15b7d101014ngm.html
view plaincopyprint?
import java.util.*;
public static void sortIntArray(int[][] arObjects, final int[] arOrders)
{
Arrays.sort(arObjects, new Comparator<Object>()
{
public int compare(Object oObjectA, Object oObjectB)
{
int[] arTempOne = (int[])oObjectA;
int[] arTempTwo = (int[])oObjectB;
for (int i = 0; i < arOrders.length; i++)
{
int k = arOrders[i];
if (arTempOne[k] > arTempTwo[k])
{
return 1;
}
else if (arTempOne[k] < arTempTwo[k])
{
return -1;
}
else
{
continue;
}
}
return 0;
}
});
}
调用方法是:
[java]
view plaincopyprint?
public static void main(String[] args)
{
int array[][] = new int[][] {
{ 12, 34, 68, 32, 9, 12, 545 },
{ 34, 72, 82, 57, 56, 0, 213 },
{ 12, 34, 68, 32, 21, 945, 23 },
{ 91, 10, 3, 2354, 73, 34, 18 },
{ 12, 83, 189, 26, 27, 98, 33 },
{ 47, 23, 889, 24, 899, 23, 657 },
{ 12, 34, 68, 343, 878, 235, 768 },
{ 12, 34, 98, 56, 78, 12, 546 },
{ 26, 78, 2365, 78, 34, 256, 873 } };
sortIntArray(array, new int[] { 0, 1 }); // 先根据第一列比较,若相同则再比较第二列
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
System.out.print(array[i][j]);
System.out.print("\t");
}
System.out.println();
}
}
注:本文转载自:http://blog.sina.com.cn/s/blog_5a15b7d101014ngm.html
相关文章推荐
- java 可以switch的类型
- 基础笔记(一)Java 集合
- JAVA动态代理 ps:模拟jdk
- Java多线程之Callable接口的实现
- java设计模式
- myeclipse 常见使用
- [译]Java Thread wait, notify和notifyAll示例
- Java 字符串数组转换为List数组
- java读取配置文件的几种方法
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- myeclipse连接sqlserver2008学习分享
- myeclipse连接sqlserver2008学习分享
- java语言的线程
- spring事务管理2
- spring 事务管理1
- java多线程基础知识点总结
- 修改myeclipse中servlet的模板
- 如何在Eclipse中查看JDK类库的源代码
- MyEclipse取消验证Js的两种途径.
- Java中Vector和ArrayList的区别