0

I've looked at the posts on here to do with sorting and searching but can't seem to get my ArrayList to sort or search using

Collections.sort(myarraylist);

The data that is stored in the ArrayList is only stored when the user enters data in text fields in a GUI. I have used getText to store the data (that the user enters into the JTextFields) in the ArrayList.

The user should be able to enter a first name or surname into a

String searchstring = JOptionPane.showInputDialog("Enter Name");

The sort should be done after the user adds each new contact into the ArrayList by surname.

So my question is:

  1. How can I sort the ArrayList?
  2. How can I search the ArrayList?

Please can you advise me on how to proceed as I am really stuck!!

Alex
  • 29
  • 5
Computeristic
  • 107
  • 1
  • 2
  • 9
  • 3
    Look at the right "Related" column. – Mārtiņš Briedis Apr 11 '11 at 23:45
  • 1
    If what's in the related column isn't working, post a small program for us that we can compile and run that illustrates the problem. – corsiKa Apr 11 '11 at 23:53
  • I have several different classes. Most of the classes contain code to build GUI's for Adding new Contacts, Search 1, Search 2, Importing and Exporing the contacts to VCARD and TXT formats. I also have a Contacts class where I have constructors that get the text entered by the user from the GUI. The arraylist is in the same class as the main menu class which consists of the main menu interface. – Computeristic Apr 11 '11 at 23:56
  • 1
    possible duplicate of [Sorting an ArrayList of Contacts](http://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts) – Brian Roach Apr 12 '11 at 00:12

2 Answers2

1

The ArrayList myarraylist is holding contacts, which I will assume has at least two String fields (first and last name). If you are comparing two contacts, how do you decide which one goes first on the sorted list?

My guess is that you have some sort of Contact type which is holding all of these fields.

What you need to do is implement the Comparable interface with the Contact type, so that the sorting algorithm can figure out which of two given contacts comes first in the list.

Where you declare your Contact type, you need to add two things: the implements Comparable declaration and the definition for your int compareTo(Object) method.

So, your Contact class will look something like this:

 public class Contact implements Comparable{
      private String firstName, lastName;
      public int compareTo(Object o){
            return lastName.compareTo(((Contact)o).getLastName());
      }
      public String getLastName(){
            return lastName;
      }
      //the rest of your Contact class stuff here.
 }
  • ... and then you need to pass the implementation as an argument to the sort method. – StriplingWarrior Apr 12 '11 at 00:07
  • Yes, I have a Contacts class. Contacts is the type that holds all the JTextFields data once the user enters the data. I have read about Comparable, but I am confused as to how I can use it in the program. Do I add it to the ActionListener for the button? Basically, I want the user to enter a name of a contact using the input box and then I want the program to search through the arraylist until it finds the contact that the user wants to find. And then I want to delete that particular contact from the arraylist. How do you think I should proceed? – Computeristic Apr 12 '11 at 00:11
0

How can I sort the arraylist?

Implement the Comparable interface and override the compareTo method for the objects going into your list.

How can I search the arraylist?

Roll your own search routine - what have you tried so far? The simplest approach is to traverse each element in the list.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124