Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: Sorting an array of objects.

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Sorting an array of objects.

    Hello! An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Please could smb help me with that? Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.
    Java Code:
    	public static Exam[] collateExams(Exam[] exams)
    	{
    		Exam [] r = new Exam[10];
    		r = exams;
    		int[] position = new int[10];
    		int index = 0;
    		
    		for(int i = 0; i < exams.length; i++)
    		{
    			if(r[i].getExamType() == 'M')
    			{   
    				
     				r[index*2] = new Exam(r[index].getFirstName(), r[index].getLastName(),
    								  r[index].getID(), r[index].getExamType(), r[index].getScore());
     			
     				position[index*2] = r[index].getID();
     				index++;
     				
     				if((r[index].getExamType() == 'F') && (position[index+1] == r[i].getID()))
     				{
     					r[index++] = new Exam(r[index++].getFirstName(), r[index++].getLastName(),
    							  r[index++].getID(), r[index++].getExamType(), r[index++].getScore());
     				}
     				/*if(position[i].getID() == r[i].getID())
     					r[i] = new Exam(r[i].getFirstName(), r[i].getLastName(),
    							  r[i].getID(), r[i].getExamType(), r[i].getScore()); 
     					*/
     				
     				
    			}
    			
    		}
    	
    		return r;

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    Do you have to write the sort algorithm yourself or can you use the Arrays class's sort() method which requires a comparator to compare two elements from the array?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Sorting an array of objects.

    Arraylist and Comparator are not allowed

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    Ok, what kind of sort are you trying to write?

    Note: The method is passed an array. There is no need (its confusing also) to assign it another variable: r.
    Used the passed name: exams
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Sorting an array of objects.

    Let's say, initially the array looks like the following:
    (0) | A B 2 midterm 100 |
    (1) | C D 1 final 90 |
    (2) | C D 1 midterm 90 |
    (3) | A B 2 final 80 |

    The method collateExams does the following:
    (0) | A B 2 midterm 100 |
    (1) | A B 2 final 80 |
    (2) | C D 1 midterm 90 |
    (3) | C D 1 final 90 |

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    What sort algorithm are you going to use to do the sort?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Sorting an array of objects.

    I'm trying to match each 'M' of the person with his ID and 'F'

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    Have you ever heard of the bubble sort? That is an easy to code algorithm.
    You need to be able to compare two elements in the array and determine which one should come first.
    Based on the results you swap them to move the one that should be "first" one before the other one. Continue until all are in order.
    You should do some research on how the bubble sort works. My comments may not be enough to code from.
    ssk2659 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Sorting an array of objects.

    Is the bubble sort possible with only a single loop in the whole method?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    only a single loop
    Is that another restriction? What more restrictions are there?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Sorting an array of objects.

    Quote Originally Posted by ssk2659 View Post
    Is the bubble sort possible with only a single loop in the whole method?
    It would be easier for you to search the web for that than get an explanation here.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Sorting an array of objects.

    That's the only restriction. Professor gave a hint to use an auxiliary array to keep track of where Final Exams appear during the single loop and whether they have been seen when a Midterm Exam shows. For instance, if position is an array of integers, then position[ID] is a convenient place for storing information about the first exam for student ID. You may want to loop over this auxiliary array and the exams array, at the same time.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Sorting an array of objects.

    Did your Professor give some stronger hints and examples? This seems a bit advanced.
    Some other unstated restrictions; The student id numbers (the index to the other array) would have a range less than what? For example 1 to 9999999999999? You need to know that to define an array.

    If there can only be one loop, how would the data that has been collected into this other array be processed?
    It would take a loop to go through its contents.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Sorting an array of objects.

    Seems to me that if you are passed an array of Exam objects which contain the student ID and the test designation, you could store the given exam as follows:

    Store the midterm grade at 2*ID and store final grade at 2*ID + 1. This assumes that an ID is based at 0. But that presumes I understand what you are trying to do (which may not be the case).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 07-31-2014, 09:47 AM
  2. Sorting objects in a custom list
    By Parashurama in forum New To Java
    Replies: 17
    Last Post: 09-14-2012, 05:40 PM
  3. Sorting objects
    By zelenkooo in forum New To Java
    Replies: 4
    Last Post: 06-11-2012, 02:43 PM
  4. Replies: 8
    Last Post: 08-13-2011, 06:02 PM
  5. sorting objects in java
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •