I have a list of values from 1 to 10, and I want to sort those values in the order 6, 7, 8, 1, 2, 0, 3, 9, 5, 10. How can I sort like this using a comparator?
import java.util.ArrayList;
import java.util.List;
public class SortTest {
public static void main(String[] args) {
int a[] = { 6, 7, 8, 1, 2, 0, 3, 9, 4, 5, 10 };
List list1 = new ArrayList(20);
List list = new ArrayList(20);
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(10);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < list.size(); j++) {
if (a[i] == (Integer) list.get(j)) {
list1.add(list.get(j));
}
}
}
System.out.println(list1);
}
}