//SortObject.java
public class SortObject {
private int i, k;
public SortObject(int i, int k) {
this.i = i;
this.k = k;
}
public int getI() {
return i;
}
public int getK() {
return k;
}
@Override
public String toString() {
return "i: " + i + "\tk: " + k;
}
}
//MyComparator.java
import java.util.Comparator;
public class MyComparator implements Comparator {
@Override
public int compare(SortObject o1, SortObject o2) {
return o1.getI() - o2.getI();
}
}
//UserCompara.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class UserCompara {
ArrayList list = new ArrayList();
public UserCompara() {
Random rnd = new Random();
for(int i = 0; i < 10; i++)
list.add(new SortObject(rnd.nextInt(100), rnd.nextInt(100)));
}
public void sort() {
list.sort(new MyComparator());
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Iterator it = list.iterator();
while(it.hasNext()) {
sb.append(it.next());
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args) {
UserCompara uc = new UserCompara();
System.out.println(uc);
uc.sort();
System.out.println(uc);
}
}