English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am making a address book in Java. I take the user input and store it into an object called entry. I then take the object and add it to an arraylist called entry_list. Each object has a method called getLName() that recalls the last name of the entry object. How would I sort the arraylist by last name?

2007-11-30 02:34:49 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

Wow - it's been a while, but I did this exact thing in college, and if I remember correctly, I had to basically "create" my own sort method, becuase arraylists hold objects which don't lent themselves to sorting too well. Now, I can't remember if their is a native "sort" method for arraylists in Java ( I don't think there is). If there is, it would have to be overridden. If not, you would need to create a method from scratch, and pass the arraylist to it. In the method, you would need to go through each object in the list, and do sorting algorithm based on the last name property of each object. I don't have acccess to my source code now, but if you get stuck, let me know, and I will check it out when I get home. Good luck.

2007-11-30 02:52:26 · answer #1 · answered by Anonymous · 0 0

Take a look at this example from Java Almanac. If you work with a Collections object, you get the sort() method.

2007-11-30 10:50:20 · answer #2 · answered by Jim Maryland 7 · 0 0

problematic matter. search over google. this could help!

2014-11-07 00:12:44 · answer #3 · answered by ? 3 · 0 0

check this out:

http://mindprod.com/jgloss/sort.html

2007-11-30 11:20:43 · answer #4 · answered by coder 1 · 0 0

fedest.com, questions and answers