fork download
  1. import java.util.*;
  2.  
  3. class Main{
  4. String[] name;
  5. int[] height;
  6. int[] etc1;
  7. int[] etc2;
  8.  
  9. public static void main(String[] args){
  10. Main m = new Main();
  11. System.out.println("---初期状態----");
  12. m.print();
  13. System.out.println("---height順----");
  14. m.sortHeight();
  15. m.print();
  16. System.out.println("---name順------");
  17. m.sortName();
  18. m.print();
  19. }
  20.  
  21. public Main(){
  22. name = new String[]{"A", "B", "C"};
  23. height = new int[]{200, 300, 100};
  24. etc1 = new int[]{20, 30, 10 };
  25. etc2 = new int[]{2, 3, 1 };
  26. }
  27.  
  28. public void print(){
  29. for(int i=0; i<height.length; i++){
  30. System.out.println(name[i]+":"+height[i]+" "+etc1[i]+" "+etc2[i]);
  31. }
  32. }
  33.  
  34. RowRef[] makeRows(){
  35. ColSet cs = new ColSet(name, height, etc1, etc2);
  36. RowRef[] rows = new RowRef[name.length];
  37. for(int i=0; i<name.length; i++){
  38. rows[i] = new RowRef(cs, i);
  39. }
  40. return rows;
  41. }
  42. void sortHeight(){
  43. RowRef[] rows = makeRows();
  44. Arrays.sort(rows, new Comparator<RowRef>(){
  45. public int compare(RowRef o1, RowRef o2){return o1.cs.height[o1.row]-o2.cs.height[o2.row];}
  46. });
  47. rebuild(rows);
  48. }
  49. void sortName(){
  50. RowRef[] rows = makeRows();
  51. Arrays.sort(rows, new Comparator<RowRef>(){
  52. public int compare(RowRef o1, RowRef o2){return o1.cs.name[o1.row].compareTo(o2.cs.name[o2.row]);}
  53. });
  54. rebuild(rows);
  55. }
  56. void rebuild(RowRef[] rows){
  57. String[] name = new String[rows.length];
  58. int[] height = new int[rows.length];
  59. int[] etc1 = new int[rows.length];
  60. int[] etc2 = new int[rows.length];
  61. for(int i=0; i<rows.length; i++){
  62. name [i] = rows[i].cs.name [rows[i].row];
  63. height[i] = rows[i].cs.height[rows[i].row];
  64. etc1 [i] = rows[i].cs.etc1 [rows[i].row];
  65. etc2 [i] = rows[i].cs.etc2 [rows[i].row];
  66. }
  67. this.name = name ;
  68. this.height = height;
  69. this.etc1 = etc1 ;
  70. this.etc2 = etc2 ;
  71. }
  72. }
  73.  
  74. class ColSet{
  75. public String[] name;
  76. public int[] height;
  77. public int[] etc1;
  78. public int[] etc2;
  79. public ColSet(String[] name, int[] height, int[] etc1, int[] etc2){
  80. this.name=name;
  81. this.height=height;
  82. this.etc1=etc1;
  83. this.etc2=etc2;
  84. }
  85. }
  86.  
  87. class RowRef{
  88. public ColSet cs;
  89. public int row;
  90. public RowRef(ColSet cs, int row){this.cs=cs; this.row=row;}
  91. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
---初期状態----
A:200 20 2
B:300 30 3
C:100 10 1
---height順----
C:100 10 1
A:200 20 2
B:300 30 3
---name順------
A:200 20 2
B:300 30 3
C:100 10 1