You want them to be all inside SorterDemo
class, well you can put sortString
class and sortStudent
class inside SorterDemo
class as nested classes(you put static
before them).
A better approach though is to have something like this :
Class SorterDemo
{
static void sortString(Student[] stud)
{
Arrays.sort(stud,Student.namecomparator);
}
static void sortStudent(Student[] stud)
{
Arrays.sort(stud,Student.markscomparator);
}
public static void main(String[] args)
{
Student student[] = new Student[3];
// initializing unsorted double array
double dArr[] = { 3.2, 1.2, 9.7, 6.2, 4.5 };
// let us print all the elements available in list
for (double number : dArr) {
System.out.println("Number = " + number);
}
// sorting array
Arrays.sort(dArr);
// let us print all the elements available in list
System.out.println("The sorted double array is:");
for (double number : dArr) {
System.out.println("Number = " + number);
}
student[0] = new Student();
student[0].setName("Nick");
student[0].setmarks(19);
student[1] = new Student();
student[1].setName("Helen");
student[1].setmarks(12);
student[2] = new Student();
student[2].setName("Ross");
student[2].setmarks(16);
System.out.println("Order of students before sorting is: ");
for (int i = 0; i < student.length; i++) {
System.out.println(student[i].getName() + "\t" + student[i].getmarks());
}
sortStudent(student);
System.out.println("Order of students after sorting by student marks is");
for (int i = 0; i < student.length; i++) {
System.out.println(student[i].getName() + "\t" + student[i].getmarks());
}
sortString(student);
System.out.println("Order of students after sorting by student name is");
for (int i = 0; i < student.length; i++) {
System.out.println(student[i].getName() + "\t" + student[i].getmarks());
}
}
}
class Student
{
static Comparator<Student> namecomparator=(s1,s2)->s1.getName().compareTo(s2.getName());
static Comparator<Student> markscomparator=(s1,s2)->Integer.compare(s1.getmarks(),s2.getmarks());
//the rest of class
}
As you can see We're defining two comparators as static fields of Student class, this talks like "we have two ways to compare Students : by name and by marks", we assign each comparator a lambda expression.
A lambda expression makes it easy to add implementations for interfaces that has only one method to implement(like Comparator) the syntax is like this : (par1,par2,...)->expr
or (par1,par2,...)->{//block of code}
, interface Runnable for example only has one method which is void run
so we can define a Runnable variable like this : Runnable r=()->{//implementation for run method}
.
Note that lambda expressions were introduced in java 8, in java 7 you use anonymous classes(classes without name) like this :
static Comparater<Student> namecomparator=new Comparator<Student>
{
@Override
public int compare(Student s1,Student s2)
{
return s1.getName().compareTo(s2.getName());
}
};
lambda expressions or anonymous classes relieves us from defining classes just for purpose of implementing interfaces, you must use anonymous classes even in java 8 if the interface has more than one method to implement.
It is reasonable to define a named class that implements interface A
only when that class has many things besides what A
has, that's my opinion at least :).
Note
There is Comparable
interface, you can make Student class implement that interface, more on the difference between Comparable and Comparator can be found here