当前位置:   article > 正文

java二维数组排序_java 二维数组排序

java 二维数组排序
  1. /* return -1 if a is before b
  2. * 1 if a is after b
  3. * 0 if a equals b */
  4. public static int compareArray(String a[], String b[])
  5. {
  6. if(Integer.parseInt(a[1]) < Integer.parseInt(b[1])) return 1;
  7. else if(Integer.parseInt(a[1]) > Integer.parseInt(b[1])) return -1;
  8. return 0;
  9. }
  10. // given two row indices, swap two rows in the matrix
  11. public static void swap(String[][] list, int a, int b)
  12. {
  13. String[] alist = list[a].clone();
  14. for(int i=0; i < list[b].length; i++)
  15. {
  16. list[a][i] = list[b][i];
  17. }
  18. for(int i=0; i < alist.length; i++)
  19. {
  20. list[b][i] = alist[i];
  21. }
  22. }
  23. /**
  24. *
  25. * sort:排序印象
  26. *
  27. * @param list
  28. * @return
  29. * @since 翔龙飞天注释模板1.0
  30. */
  31. public static String[][] sort(String[][] list)
  32. {
  33. int len = list.length;
  34. int min, comp;
  35. for(int i=0; i < len; i++)
  36. {
  37. min = i;
  38. for(int j=i+1; j < len; j++)
  39. {
  40. comp = compareArray(list[min], list[j]);
  41. if(comp == 1) min = j;
  42. }
  43. swap(list, i, min);
  44. }
  45. return list;
  46. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/995526
推荐阅读
相关标签
  

闽ICP备14008679号