
11-26-2009, 10:20 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 9
Rep Power: 0
|
|
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
|
|

11-26-2009, 11:05 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
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
|
|

11-26-2009, 11:09 PM
|
|
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
|
|

11-26-2009, 11:15 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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.
|
|

11-26-2009, 11:19 PM
|
|
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.
|
|

11-26-2009, 11:23 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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
|
|

11-26-2009, 11:32 PM
|
|
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
|
|

11-26-2009, 11:34 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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
|
|

11-26-2009, 11:38 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 9
Rep Power: 0
|
|
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();
} |
|
|

11-26-2009, 11:58 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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
|
|

11-27-2009, 03:58 AM
|
|
Senior Member
|
|
Join Date: Jul 2009
Posts: 231
Rep Power: 1
|
|
|
|
|

11-27-2009, 04:20 AM
|
|
Senior Member
|
|
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 263
Rep Power: 1
|
|
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"
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 04:36 PM.
|
|