Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 10:20 PM
SBL SBL is offline
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
SBL is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-26-2009, 11:05 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2009, 11:09 PM
SBL SBL is offline
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
SBL is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 11:15 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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:
Code:
      Collections.sort(book, new Comparator<Contact>() {

         public int compare(Contact p1, Contact p2) {
            return p1.getsecondName().compareToIgnoreCase(p2.getsecondName());
         }
      });
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags

Last edited by Fubarable; 11-26-2009 at 11:19 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-26-2009, 11:19 PM
SBL SBL is offline
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
SBL is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-26-2009, 11:23 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
You either need to import java.util.Comparator or use fully qualified names, i.e.,
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());
         }
      });
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-26-2009, 11:32 PM
SBL SBL is offline
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
SBL is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-26-2009, 11:34 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Please see post #4 in this thread. To quote:
Quote:
We may also need to see more of your code. Finally, please don't forget to use code tags when posting code.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-26-2009, 11:38 PM
SBL SBL is offline
Member
 
Join Date: Nov 2009
Posts: 9
Rep Power: 0
SBL is on a distinguished road
Default
This is my methods that gets the data from a file
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();
    }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-26-2009, 11:58 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-27-2009, 03:58 AM
Senior Member
 
Join Date: Jul 2009
Posts: 231
Rep Power: 1
camickr is on a distinguished road
Default
BeanComparator
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-27-2009, 04:20 AM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 263
Rep Power: 1
zweibieren is on a distinguished road
Default
Note that "collections" is not capitalized in the error message.
The class you need to use is called Collections with a capital C.

Quote:
"collections is not a known variable in the current context"
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vector<vector> loop thru ocean New To Java 11 11-21-2009 03:17 PM
How to store data from textfile to vector and delete a selected row. nemesis New To Java 15 11-13-2009 12:00 PM
sorting data in txt file cassysumandak New To Java 1 04-12-2009 04:02 AM
Data Sorting in a .data file using java stutiger99 New To Java 2 10-08-2008 03:52 AM
URGENT: Sorting a vector of object by an element doobybug New To Java 1 03-12-2008 07:37 PM


All times are GMT +2. The time now is 04:36 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org