Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- public class Algorithms {
- private static class Student implements Comparable<Student> {
- private float grade;
- private String name;
- @Override
- public int compareTo(Student student) {
- return Float.compare(grade, student.grade);
- }
- public float getGrade() {
- return grade;
- }
- public String getName() {
- return name;
- }
- public Student(String name, float grade) {
- this.grade = grade;
- this.name = name;
- }
- @Override
- public String toString() {
- return "Student{" +
- "grade=" + grade +
- ", name='" + name + '\'' +
- '}';
- }
- }
- private static void printStudents(Collection<Student> collection) {
- for (Student student : collection) {
- System.out.println(student);
- }
- }
- public static void main(String[] args) {
- ArrayList<Student> students = new ArrayList<>();
- students.add(new Student("Serban", 9.3f));
- students.add(new Student("Nicolae", 5.3f));
- students.add(new Student("Ion", 7.3f));
- System.out.println("Initial");
- printStudents(students);
- Collections.sort(students);
- System.out.println("Sortati dupa nota");
- printStudents(students);
- Collections.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student student, Student t1) {
- return -student.getName().compareTo(t1.getName());
- }
- });
- System.out.println("Sortati descrescator dupa nume");
- printStudents(students);
- }
- }
Add Comment
Please, Sign In to add comment