Results 1 to 11 of 11
  1. #1
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Sorting an Array of Objects with multiple fields

    Currently I am working on a contact manager project where I have a contactMgr class, and a contact class within the contactMgr class that stores a contacts info.

    To start out the program, I create a contactMgr array with 20 indices. Then, when I create a contact, that includes name, email and phone number, I then store it in the contactMgr array. All of the fields for the contact object are stored as Strings.

    How can I sort the contact objects in the contactMgr array alphabetically by name?


    This is pretty much how the classes are setup. The Contact class is made inside the public class ContactMgr.

    Java Code:
    public class ContactMgr
    {
    	// ****** global variables
    	final int MAX_CONTACTS = 20;
    	
    	Contact[] contactList = new Contact[MAX_CONTACTS];
            
    
            Arrays.sort(contactList);
    
    
    
    
            class Contact
            {
                  String name = "";
                  String email = "";
                  String phone = "";

  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 with multiple fields

    How can I sort the contact objects in the contactMgr array alphabetically by name?
    Use the Arrays sort() method that uses a Comparator which will look at instances of your class objects and say what order they should be in.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    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 with multiple fields

    Your Contact class needs to implement the Comparable interface which has a compareTo method. Check out the Java API for details. The tutorials
    in my signature below this post may also help.

    Also, it does not appear that you have a contactMgt array but a Contact array.

    Regards,
    Jim
    Last edited by jim829; 10-16-2016 at 03:20 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Sorting an Array of Objects with multiple fields

    Quote Originally Posted by jim829 View Post
    Also, it does not appear that you have a contactMgt array but a Contact array.

    Whoops, can't believe I missed that, thank you for pointing it out. So only the Contact class would need to implement the comparable interface? Or do I need to implement it for ContactMgr as well?

  5. #5
    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 with multiple fields

    No just the class that is being sorted. The Arrays.sort method will then call the compareTo method on each Contact object and sort based
    on your implementation.

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

  6. #6
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Sorting an Array of Objects with multiple fields

    Ok, so I tried tinkering with it, and looked up some tutorials, but I still cannot get it to work. I keep getting this error:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
    	at java.util.ComparableTimSort.sort(Unknown Source)
    	at java.util.Arrays.sort(Unknown Source)
    	at ContactMgr.addContact(ContactMgr.java:114)
    	at ContactMgrTest.main(ContactMgrTest.java:13)

  7. #7
    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 with multiple fields

    You need to show your code.

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

  8. #8
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Sorting an Array of Objects with multiple fields

    I actually figured out what I was doing wrong. I was trying to sort an Array with a capacity of 20 elements, that only had 3 in it. Was not aware that you cannot call sort on array that contains null indices.

  9. #9
    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 with multiple fields

    Look at the API doc for the sort() method. It has args for specifying a range of elements to sort.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    pilpod is offline Member
    Join Date
    Aug 2016
    Posts
    9
    Rep Power
    0

    Default Re: Sorting an Array of Objects with multiple fields

    Ahhh, thank you for pointing that out. I completely overlooked that while looking through the documentation.

  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 with multiple fields

    Which is another reason you might want to consider using a List implementation (e.g. ArrayList). The size is managed dynamically. And you can sort
    a Collection (which is a super type of List) using Collections.sort().

    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. Sorting an array of objects.
    By ssk2659 in forum New To Java
    Replies: 13
    Last Post: 02-14-2015, 11:04 PM
  2. How to add multiple objects/atributes into an array?
    By HEIMDAL13 in forum New To Java
    Replies: 13
    Last Post: 12-24-2013, 10:56 PM
  3. Sorting a Vector by Fields of Objects Stored in it
    By Willriker in forum New To Java
    Replies: 10
    Last Post: 06-15-2013, 04:18 PM
  4. Replies: 8
    Last Post: 08-13-2011, 06:02 PM
  5. Sorting/Searching Objects with multiple types.
    By gcampton in forum New To Java
    Replies: 20
    Last Post: 10-21-2009, 11:58 PM

Posting Permissions

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