Java object array sorting and java array sorting
Import java. util .*;
Import java. io .*;
Public class Main {
Static int [] dp = new int [1010];
Public static void main (String [] args) throws IOException {
Mouse [] mice = new Mouse [1010];
FileReader fr = new FileReader ("in.txt"); // read the file
BufferedReader read = new BufferedReader (fr );
String str = "";
Int n = 1;
While (str = read. readLine ())! = Null ){
String [] s = str. split ("");
Mice [n] = new Mouse ();// Object Instantiation, very important
Mice [n]. weight = Integer. parseInt (s [0]);
Mice [n]. speed = Integer. parseInt (s [1]);
N ++;
}
System. out. println (n );
Arrays. sort (mice, 1, n );// Sort (int start, int end) includes the start index, excluding the end index.
For (int I = 1; I <n; I ++ ){
System. out. println (mice [I]. weight + "" + mice [I]. speed );
}
}
}
Class Mouse implements Comparable {// Implement the Comparable Interface
Int weight;
Int speed;
Public int compareTo (Object o ){// Override the compareTo Method
Mouse m = (Mouse) o;
Return weight> m. weight? 1 :( weight = m. weight? 0:-1 );
}
}