2

How can I sort the name of student on basis of their marks? The marks of student is provided. Can I use Collection class in java for the sorting? Should I import something?

import java.util.*;

class ShortRecord {

   public static void main(String args[]) {

      // Create a hash map
      HashMap hm = new HashMap();
      // Put elements to the map
      hm.put("Zara", new Double(3434.34));
      hm.put("Mahnaz", new Double(123.22));
      hm.put("Ayan", new Double(1378.00));
      hm.put("Daisy", new Double(99.22));
      hm.put("Qadir", new Double(-19.08));

      // Get a set of the entries
      Set set = hm.entrySet();
      // Get an iterator
      Iterator i = set.iterator();
      // Display elements
      while (i.hasNext()) {
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }

   }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Vishwa Pratap
  • 374
  • 6
  • 14

3 Answers3

3

If you want to sort by the Map's key (i.e. name), then use a java.util.TreeMap.

If you want to sort based on value, then see Sort a Map<Key, Value> by values (Java)

Community
  • 1
  • 1
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
2

I think you should create a POJO Student class, and implement Comparable<Student> - then put those Students in a List and then Collections.sort() it. So, something like

static class Student implements Comparable<Student> {
  public Student(String name, double grade) {
    this.name = name;
    this.grade = grade;
  }

  String name;
  double grade;

  @Override
  public int compareTo(Student o) {
    if (o == null) {
      return -1;
    }
    int c = Double.valueOf(grade).compareTo(o.grade);
    if (c != 0) {
      return c;
    }
    return name.compareTo(o.name);
  }

  @Override
  public String toString() {
    return String.format("%s has grade %.2f", name, grade);
  }
}

public static void main(String[] args) {
  List<Student> al = new ArrayList<>();
  al.add(new Student("Zara", 3434.34));
  al.add(new Student("Mahnaz", 123.22));
  al.add(new Student("Ayan", 1378.00));
  al.add(new Student("Daisy", 99.22));
  al.add(new Student("Qadir", -19.08));
  Collections.sort(al);
  System.out.println(al);
}

Output is (formatted) -

[Qadir has grade -19.08, Daisy has grade 99.22, 
 Mahnaz has grade 123.22, Ayan has grade 1378.00, 
 Zara has grade 3434.34]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Another example this time using a custom comparator:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Marks
{
    static class SortByMark implements Comparator<Student>
    {
        @Override
        public int compare(Student studentOne, Student studentTwo)
        {
            return studentOne.mark.compareTo(studentTwo.mark);
        }
    }

    static class Student
    {
        private final Double mark;
        private final String name;

        public Student(String name, Double mark)
        {
            this.name = name;
            this.mark = mark;
        }

        public Double getMark()
        {
            return mark;
        }

        public String getName()
        {
            return name;
        }

        @Override
        public String toString()
        {
            return name + ": " + mark;
        }
    }

    private final static SortByMark sortByMark = new SortByMark();;

    public static void main(String args[])
    {
        List<Student> students = new ArrayList<Student>();

        students.add(new Student("Zara", new Double(3434.34)));
        students.add(new Student("Mahnaz", new Double(123.22)));
        students.add(new Student("Ayan", new Double(1378.00)));
        students.add(new Student("Daisy", new Double(99.22)));
        students.add(new Student("Qadir", new Double(-19.08)));

        Collections.sort(students, sortByMark);

        for (Student student : students)
        {
            System.out.println(student);
        }
    }
}
fipple
  • 360
  • 1
  • 5