JDK Sorting Give the calls to the java Arrayssort needed to
JDK Sorting. Give the calls to the java Arrays.sort() needed to sort the following. Read the JDK documentation for all the possible sort prototypes.
(a) String[] names = {“Joe”, ”Annie”, “Bruce”, ”Tanya”};
(b) int[] numbers = {3,2,10};
(c) names again, but in reverse order (use Collections.ReverseComparator)
(d) SimpleStudent students = {new SimpleStudent(“Bob”,0), new SimpleStudent(“Joe”, 1), new SimpleStudent(“Bob”,2)} using their natural order.
(e) students again, but using their ids. This is the only part of this problem where you need to write more than one line of code.
Solution
PROGRAM CODE:
package Simple;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class SimpleStudent implements Comparable<SimpleStudent>{
String name;
int id;
public SimpleStudent(String name, int id) {
this.name = name;
this.id = id;
}
// for sorting the id of students
@Override
public int compareTo(SimpleStudent o) {
int compareId = o.id;
// ascending order
return this.id - compareId;
}
// for sorting the names of Students
public static Comparator<SimpleStudent> StudentNameComparator = new Comparator<SimpleStudent>() {
public int compare(SimpleStudent stud1, SimpleStudent stud2) {
String fruitName1 = stud1.name;
String fruitName2 = stud2.name;
//ascending order
return fruitName1.compareTo(fruitName2);
//descending order
//return fruitName2.compareTo(fruitName1);
}
};
public static void main(String args[])
{
//using Arrays.sort()
//a) sorting of names
String[] names = {\"Joe\", \"Annie\", \"Bruce\", \"Tanya\"};
Arrays.sort(names); // one line of code to sort the array
// This is just for printing the elements in sorted order
System.out.print(\"a) \");
for(int i=0; i<names.length; i++)
{
System.out.print(names[i]+\" \");
}
System.out.println(\"\ \");
//b) sorting numbers
int[] numbers = {3,2,10};
Arrays.sort(numbers); // one line of code to sort the array
// This is just for printing the elements in sorted order
System.out.print(\"b) \");
for(int i=0; i<numbers.length; i++)
{
System.out.print(numbers[i]+\" \");
}
System.out.println(\"\ \");
//c) names in reverse order
Arrays.sort(names, Collections.reverseOrder()); // one line of code to sort the array
// This is just for printing the elements in sorted order
System.out.print(\"c) \");
for(int i=0; i<names.length; i++)
{
System.out.print(names[i]+\" \");
}
System.out.println(\"\ \");
//d) natural order sorting for names
SimpleStudent[] student = {new SimpleStudent(\"Bob\", 0), new SimpleStudent(\"Joe\", 1), new SimpleStudent(\"Bob\", 2)};
Arrays.sort(student, SimpleStudent.StudentNameComparator);// one line of code to sort the array
// This is just for printing the elements in sorted order
System.out.print(\"d) \");
for(int i=0; i<student.length; i++)
{
System.out.println(student[i].name+\" \" + student[i].id);
}
System.out.println(\"\ \");
//e) sorting student based on id
Arrays.sort(student);
// This is just for printing the elements in sorted order
System.out.print(\"d) \");
for(int i=0; i<student.length; i++)
{
System.out.println(student[i].name+\" \" + student[i].id);
}
System.out.println(\"\ \");
}
}
OUTPUT:
a) Annie Bruce Joe Tanya
b) 2 3 10
c) Tanya Joe Bruce Annie
d) Bob 0
Bob 2
Joe 1
d) Bob 0
Joe 1
Bob 2



