I have a pretty basic assignment that requires me to create a LinkedList of stri
ID: 3534698 • Letter: I
Question
I have a pretty basic assignment that requires me to create a LinkedList of strings. I need to fill the LinkedList with names on a file called "names.txt". I also need to add an insertion sort and sort the LinkedList. Finally, print it on the screen. The text file (in notepad) has the following fifty names: Kimber Hord Evalyn Lipscomb Adeline Mazza Tammi Perkinson Mazie Coolbaugh Janae Roquemore Natashia Wheelock Daria Callison Jessia Rondon Elois Simpson Gordon Summerfield Lino Wightman Shirlee Guan Jaimie Bagby Delmer Witty Ardis Nott Joslyn Quiroz Marquita Tryon Laurence Landreth Caterina Solum Randee Burbridge Danielle Bergin Emory Humes Helga Cheney Sherwood Ros Kelvin Carroway Sylvie Tessman Alyse Hott Gilberte Argueta Glenna Gold Sonya Saleem Maren Modlin Patrice Cropp Lionel Ezzell Sherri Fly Weston Blackshire Stacia Quintanilla Laurie Lesko Marylin Mcgough Delpha Sisneros Louie Angus Yung Shuler Karin Picha Junior Schleusner Ashely Sandavol Wai Guth Corey Bechtold Ai Slemp Eladia Ellenburg Stephany TroutExplanation / Answer
Please rate with 5 stars :)
In order to sort Strings alphabetically you will need to use a Collator, like:
LinkedList<String> list = new LinkedList<String>();
list.add("abc");
list.add("Bcd");
list.add("aAb");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Collator.getInstance().compare(o1, o2);
}
});
Because if you just call Collections.sort(list) you will have trouble with strings that contain uppercase characters.
For instance in the code I pasted, after the sorting the list will be: [aAb, abc, Bcd] but if you just call Collections.sort(list); you will get: [Bcd, aAb, abc]
Note: When using a Collator you can specify the locale Collator.getInstance(Locale.ENGLISH) this is usually pretty handy.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.