/* return -1 if a is before b
* 1 if a is after b
* 0 if a equals b */
public static int compareArray(String a[], String b[])
{
if(Integer.parseInt(a[1]) < Integer.parseInt(b[1])) return 1;
else if(Integer.parseInt(a[1]) > Integer.parseInt(b[1])) return -1;
return 0;
}
// given two row indices, swap two rows in the matrix
public static void swap(String[][] list, int a, int b)
{
String[] alist = list[a].clone();
for(int i=0; i < list[b].length; i++)
{
list[a][i] = list[b][i];
}
for(int i=0; i < alist.length; i++)
{
list[b][i] = alist[i];
}
}
/**
*
* sort:排序印象
*
* @param list
* @return
* @since 翔龙飞天注释模板1.0
*/
public static String[][] sort(String[][] list)
{
int len = list.length;
int min, comp;
for(int i=0; i < len; i++)
{
min = i;
for(int j=i+1; j < len; j++)
{
comp = compareArray(list[min], list[j]);
if(comp == 1) min = j;
}
swap(list, i, min);
}
return list;
}
java二维数组排序
最新推荐文章于 2024-07-07 11:58:34 发布