Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java

    Hi
    I have asked for a question that is we have a class named Student that has some fields like name,age and date of birth.
    i have to write a programm that can sort the list of students on the basis of his name,age and date of birth.
    if name is same than on the basis of age, if name and age are same then on the basis of date of birth.
    could anyone pls help me by giving the code for that?

    Thanks for help in advance.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java

    No, users here will not 'give you the code.' Start the program yourself and ask for help as you need it.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java

    Now giving the code:

    class Students
    {
     String name;
     int age;
     Date dob;
    }
     class  SortList
      {
       Student st;
      List <Students>  raw_sts=new Arraylist<Students>();
      List <Students>  sorted_sts=new Arraylist<Students>();
     
     
         public static void main(String arr[])
         {
              st=new Students();
             sorted_sts= Collections.sort(sts,st.name); //here i am little bit confuse what to give next to this line; that i want to be clarified.
                                                                       bcz it will sort on the basis of name only.rest condition would not be fullfilled.
     
       }
    }
    Last edited by jps; August 11th, 2013 at 09:52 AM. Reason: code tags

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java

    Please post your code in code or highlight=java tags.

    Comments:

    The class is name 'Students', so the declaration should be "Students st;" (with an 's' at the end of Student).
    ArrayList is capital 'A', capital 'L'
    In order to use the variables st, raw_sts, and sorted_sts in the static method main(), the variables must be declared static.

    As for your sorting question, you may want to use Collections.sort() as you have already done (with corrected arguments), then look for those that have the same last names, sort those by age; then look for those with same last name and same age and sort those by DOB.

    To accomplish the sorting of the Students sub-Lists, you could write your own sorter from the start; write a sorter that handles the duplicate cases after the main sorting has been done; or identify the specific special cases (same last name, etc.), create a special sub-List of just those items, and reform/re-sort those using Collections.sort() or the other approach you've used for the main case.

    I don't have an opinion about which is easiest. I would be inclined to write my own sorter from the start or use Collections.sort() to do the heavy lifting and then augment those results with my own sorter, leaning towards the latter.

  5. #5
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Java

    Hello.
    You class Students can implement the java.util.Comparable interface and override the compareTo() method.
    In this method, write the logic based on your sorting requirements.
    Then you can invoke Collections.sort() or Arrays.sort() method.

    Syed.

Tags for this Thread