Results 1 to 12 of 12
Thread: Sorting data in a Vector
- 11-26-2009, 09:20 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
-
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.
- 11-26-2009, 10:09 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
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
-
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.
- 11-26-2009, 10:19 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
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.
-
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()); } });
- 11-26-2009, 10:32 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
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
-
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.
- 11-26-2009, 10:38 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
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(); }
-
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-27-2009, 02:58 AM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
- 11-27-2009, 03:20 AM #12
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Similar Threads
-
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
How to store data from textfile to vector and delete a selected row.
By nemesis in forum New To JavaReplies: 15Last Post: 11-13-2009, 11:00 AM -
sorting data in txt file
By cassysumandak in forum New To JavaReplies: 1Last Post: 04-12-2009, 03:02 AM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM -
URGENT: Sorting a vector of object by an element
By doobybug in forum New To JavaReplies: 1Last Post: 03-12-2008, 06:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks