您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: