Menu
Question Index
...

Write a program to sort the students by name in descending order.
HINT: Create an anonymous class implementing the Comparator interface for doing this.


import java.util.*;
class SortStudentsByNameDescending
{
    public static void main(String s[])
    {
        Student anand = new Student("Anand", 5, 87, 'A');
        Student saroja = new Student("Saroja", 6, 91, 'A');
        Student aswini = new Student("Aswini", 9, 55, 'C');
        Student manthra = new Student("Manthra", 10, 76, 'B');
        Student satya = new Student("Satya", 5, 76, 'C');
        Student bhadra = new Student("Bhadra", 4, 71, 'C');
        Student venki = new Student("Venki", 8, 45, 'B');
        Student kanchana = new Student("Kanchana", 2, 39, 'B');
        
        List<Student> students = new ArrayList<Student>();
        students.add(anand);
        students.add(saroja);
        students.add(aswini);
        students.add(manthra);
        students.add(satya);
        students.add(bhadra);
        students.add(venki);
        students.add(kanchana);
        
        
        sortStudentsByNameDesc(students);
        
        for(int i = 0; i < students.size(); i++)
        {
            students.get(i).print();
        }

    }


    public static void sortStudentsByNameDesc(List<Student> students)
    {
    }

}
class Student
{
    String name;
    int rollNumber;
    int marks;
    char section;

    Student(String name, int rollNumber, int marks, char section)
    {
        this.name = name;
        this.rollNumber = rollNumber;
        this.marks = marks;
        this.section = section;
    }

    void print()
    {
        System.out.println(section + " - " +rollNumber + " - " + name + " - " + marks );
    }
}

Doubts

Problems

Topic: Java Collection Algorithms

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App