Results 1 to 12 of 12
  1. #1
    SBL
    SBL is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default Sorting data in a Vector

    Hi there,
    Pretty much i have created an application that reads data from a file about a person then puts it into JTextFields.
    My question is how do i sort the data so that it is sorted alphabetically before it goes into the text fields?
    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried using Collections.sort? If you use this, the objects held by the Vector must implement the Comparable interface. Either that or you'll have to create a Comparator object and use that to help you sort.

  3. #3
    SBL
    SBL is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    i tired
    Collections.sort(book, new comparator(){
    public int compare (Object o1, Object o2){
    Contact p1 = (Contact) o1;
    Contact p2 = (Contact) o2;
    return p1.getsecondName().compareToIgnoreCase (p2.getsecondName());

    but i keep getting an error on the Comparator and i cant find out why.
    cheers

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You probably want to tell us what error you get with comparator. We may also need to see more of your code. Finally, please don't forget to use code tags when posting code.

    Good luck!

    edit: note that Comparator is capitalized and can be used with Generics:
    Java Code:
          Collections.sort(book, new Comparator<Contact>() {
    
             public int compare(Contact p1, Contact p2) {
                return p1.getsecondName().compareToIgnoreCase(p2.getsecondName());
             }
          });
    Last edited by Fubarable; 11-26-2009 at 10:19 PM.

  5. #5
    SBL
    SBL is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    sorry new to all of this it say that "collections is not a known variable in the current context" im using netbeans to design a gui addressbook for an assignment.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You either need to import java.util.Comparator or use fully qualified names, i.e.,
    Java Code:
          java.util.Vector<Contact> book = new java.util.Vector<Contact>();
    
          java.util.Collections.sort(book, new java.util.Comparator<Contact>() {
    
             public int compare(Contact p1, Contact p2) {
                return p1.getsecondName().compareToIgnoreCase(p2.getsecondName());
             }
          });

  7. #7
    SBL
    SBL is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks for replying. The code doesn't error but it doesn't sort the data so its in Alphabetical order. What am i doing wrong? thanks

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please see post #4 in this thread. To quote:
    We may also need to see more of your code. Finally, please don't forget to use code tags when posting code.

  9. #9
    SBL
    SBL is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    This is my methods that gets the data from a file
    Java Code:
    public void getContacts() throws IOException
         {
                {
    
                BufferedReader file_in = new BufferedReader(new FileReader("files/Book1.buab"));
    
                int count=0;
                String name = "";
                String secondname = "";
                String phone = "";
                String mobilephone = "";
                String address = "";
                String email ="";
    
                while (true) {
                    String line = file_in.readLine();
                    switch(count){
                        case 0:
                         {
                            name = line;
                         }
                        case 1:
                        {
                            secondname = line;
                        }
                          case 2:
                        {
                            phone = line;
                        }
                          case 3:
                        {
                            mobilephone = line;
                        }
                          case 4:
                        {
                            address = line;
                        }
                            case 5:
                        {
                            email = line;
                        }
    
    
    
              
                    }
    
                    if (line == null)
                        break;
    
                    if (count == 5){
                        addContact(new Contact(name, secondname, phone, mobilephone, address, email));
                        count = 0;
                    }
                    else count++;
    
    
    
                    
                }
                
            }
    
        }
    
    /*-----------------------------------------------------------------------*/
    public void displayContact()
        {
             if(book.size() != 0)
        {
            txtFirstName.setText(book.elementAt(index).getfirstName());
            txtSecondName.setText(book.elementAt(index).getsecondName());
            txtPhone.setText(book.elementAt(index).getPhone());
            txtMobilePhone.setText(book.elementAt(index).getmobilePhone());
            txtAddress.setText(book.elementAt(index).getAddress());
            txtEmail.setText(book.elementAt(index).getEmail());
        }
            else
            clearScreen();
        }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Maybe someone else smarter than me can figure out where your problem lies, but as for myself, I don't see enough information to be able to solve this. You may want to look at this link as it may help you to post pertinent code that's germane to your problem: Short, Self Contained, Correct (Compilable), Example

    Again, much luck!

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

  12. #12
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Note that "collections" is not capitalized in the error message.
    The class you need to use is called Collections with a capital C.

    "collections is not a known variable in the current context"

Similar Threads

  1. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  2. Replies: 15
    Last Post: 11-13-2009, 11:00 AM
  3. sorting data in txt file
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 03:02 AM
  4. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  5. URGENT: Sorting a vector of object by an element
    By doobybug in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 06:37 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
  •