In: Computer Science
import java.util.*;
class A
{
int i, j, k;
public A(int i, int j, int k)
{
this.i=i;
this.j=j;
this.k=k;
}
public String toString()
{
return "A("+i+","+j+","+k+")";
}
}
class Main
{
public static void main(String[] args)
{
ArrayList<A> aL=new ArrayList<A>();
Random rand= new Random(1000); //1000 is a seed value
for (int p=0; p<10; p++)
{
int i = rand.nextInt(100);
int j = rand.nextInt(200);
int k = rand.nextInt(300);
aL.add(new A(i, j, k));
}
System.out.println("----- Original arraylist------");
for (A a: aL)
{
System.out.println(a);
}
System.out.println("----- Sorting by first integer-------");
/*YOUR CODE - Use anonymous interface types to sort by first integer Field in A, and then
print the resulting ArrayList */
System.out.println("----- Sorting by second integer-------");
/*YOUR CODE - Use anonymous interface types to sort by the second integer Field in A, and then
print the resulting ArrayList */
System.out.println("----- Sorting by third integer-------");
/*YOUR CODE - Use anonymous interface types to sort by the third integer Field in A, and then
print the resulting ArrayList */
}
}
Ideal Output:
----- Original list ------- A(87,135,276) A(24,192,149) A(41,45,164) A(50,179,259) A(72,183,36) A(75,46,202) A(23,41,222) A(71,189,202) A(93,142,49) A(42,35,176) ----- Sorting by first integer------- A(23,41,222) A(24,192,149) A(41,45,164) A(42,35,176) A(50,179,259) A(71,189,202) A(72,183,36) A(75,46,202) A(87,135,276) A(93,142,49) ----- Sorting by second integer------- A(42,35,176) A(23,41,222) A(41,45,164) A(75,46,202) A(87,135,276) A(93,142,49) A(50,179,259) A(72,183,36) A(71,189,202) A(24,192,149) ----- Sorting by
Thanks for the question, Here is the complete code in Java. Have used anonymous interface Comparator to sort the array list
=======================================================
import java.util.*;
import java.util.ArrayList;
public class A {
int i,
j, k;
public A(int
i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
public String toString()
{
return
"A(" + i + "," +
j + "," + k +
")";
}
}
class Main {
public static void
main(String[] args) {
ArrayList<A> aL =
new ArrayList<A>();
Random rand =
new Random(1000); //1000 is a seed value
for
(int p = 0; p < 10; p++) {
int i = rand.nextInt(100);
int j = rand.nextInt(200);
int k = rand.nextInt(300);
aL.add(new A(i, j, k));
}
System.out.println("----- Original
arraylist------");
for (A
a : aL) {
System.out.println(a);
}
System.out.println("----- Sorting by first
integer-------");
/*YOUR CODE - Use anonymous interface types to sort by first
integer Field in A, and then
print the resulting ArrayList */
Collections.sort(aL,
new Comparator<A>() {
@Override
public int compare(A objA, A objB) {
return objA.i -
objB.i;
}
});
for (A
a : aL) {
System.out.println(a);
}
System.out.println("----- Sorting by
second integer-------");
/*YOUR CODE - Use anonymous interface types to sort by the second
integer Field in A, and then
print the resulting ArrayList */
Collections.sort(aL,
new Comparator<A>() {
@Override
public int compare(A objA, A objB) {
return objA.j -
objB.j;
}
});
for (A
a : aL) {
System.out.println(a);
}
System.out.println("----- Sorting by third
integer-------");
/*YOUR CODE - Use anonymous interface types to sort by the third
integer Field in A, and then
print the resulting ArrayList */
Collections.sort(aL,
new Comparator<A>() {
@Override
public int compare(A objA, A objB) {
return
objA.k - objB.k;
}
});
for (A
a : aL) {
System.out.println(a);
}
}
}
==================================================================