-2

I have 10 students contains details as int enrollNo String name; long contactNo

and i want to Take input of this details and make ascending order group by enrollNo. And Print them

Code for Editing :: https://drive.google.com/file/d/0B8jFHHQnzuEiT29SQVpncUxzMzg

  • requirements != problem description. We are here to help you solve problems, not to fulfill your requirements. – Pshemo Aug 11 '16 at 18:55
  • Please [edit] your question and describe problem you are facing. Include your code, describe how it currently works and how you would like it to work instead. – Pshemo Aug 11 '16 at 18:55
  • Don't make links main source of information. If link will break your question will be useless for future visitors which means it doesn't belong here. Post your code here instead (if it is too long try to limit it by removing unnecessary things like number of fields in class, reading from user - create N objects with fixed data). – Pshemo Aug 11 '16 at 19:01
  • Have a look at [Oracle's Object Ordering tutorial](https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html). Reading through it will help you more than asking a vague question here. – Mick Mnemonic Aug 11 '16 at 19:03

1 Answers1

0

Use Comparator Interface like Below:

public class SortUserDefinedObjectUsingAnonymousComparator {

    public static void main(String[] args) {
        ArrayList<Employee> arrayList=new ArrayList<Employee>();
        arrayList.add(new Employee("Kamal", 20, 567));
        arrayList.add(new Employee("Doui", 50, 464));
        arrayList.add(new Employee("Buck", 90, 123));
        arrayList.add(new Employee("Auot", 100, 3445));


        for (Employee employee : arrayList) {
            System.out.println("ID:"+employee.getId()+" ,name:"+employee.getName());
        }

        Collections.sort(arrayList,new Comparator<Employee>() {

            public int compare(Employee empl1, Employee empl2) {

                if (empl1.getId()>empl2.getId()) {
                    return 1;
                }

                else if (empl1.getId()<empl2.getId()) {
                    return -1;
                }

                else {
                    return 0;
                }
            }
        });

        System.out.println("\n-------------------After Sort-------------------\n");
        for (Employee employee : arrayList) {
            System.out.println("ID:"+employee.getId()+" ,name:"+employee.getName());
        }

    }

}
Krishna
  • 172
  • 6