Sorting the userdefined data by name by overriding compareTo function of Interface Comparable

 


public class Student implements Comparable{

int rollNo;
int marks;
String name;

public Student(int rollNo, int marks, String name) {
this.rollNo = rollNo;
this.marks = marks;
this.name = name;
}
@Override
public String toString() {
return (name + " " + marks + " " + rollNo);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student s = (Student) obj;
return ( this.rollNo == rollNo && this.marks == marks && this.name.equals(s.name));
} else {
return false;
}
}
@Override
public int compareTo(Object obj){
if(obj instanceof Student){
Student s = (Student) obj;
return(this.name.compareTo(s.name));
}
return 0;
}
}

import java.util.*;

public class Userdefined1 {
public static void main(String[] args) {
Student s1 = new Student(1,90,"Ajay");
Student s2 = new Student(2,56,"Vijay");
Student s3 = new Student(3,76,"Kiyan");
Student s4 = new Student(4,84,"Kayna");

//Creating an arraylist to store student objects
ArrayList<Student> st = new ArrayList<>();
st.add(s1);
st.add(s2);
st.add(s3);
st.add(s4);
//To sort arraylist
Collections.sort(st);
System.out.println("Sorted ArrayList Using by Name :");
System.out.println(st);

}
}



Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...