在我使用的代码下面工作正常并输出名称,除了sort方法不起作用.我期待"Collections.sort(nameFromText);" 按名字按字母顺序对ArrayList进行排序.
我究竟做错了什么?
public static void main(String[] args) throws IOException {
// Create and populate text file
Writer textFile = new FileWriter("names.txt");
String[] nameArray = new String[] { "Tina Tully\n", "Bill Simpson\n",
"Dana Smith\n", "Ralph Andrews\n", "Greg Smithers\n",
"Lisa Krump\n", "Gill Bitters\n", "Barbara West\n",
"Sandra McDonald\n", "Bart Willis\n", "Bucky Zimmerman\n",
"Richard Vicks\n", "Velma Tarp\n", "Winslow Tunnell\n",
"Andrew Letterman\n", "Betty Trump\n", "Waldo Smith\n",
"Kyle Ronno\n", "Vivian West\n", "Wendy Tunnell\n" };
generateText(textFile, nameArray);
// Create object of previously created text file
Scanner …Run Code Online (Sandbox Code Playgroud) I have a String list of numbers (they can be an int list, just need to turn it into an array list to return), this numbers can be from a range, for example from 1 to 25.
I tried sorting them using Collections.sort(numbersList); but it sorts them in a weird way. For example this is the current sorting:
1
10
11
..
2
20
21
..
Run Code Online (Sandbox Code Playgroud)
What I really want is for it to be sorted in a numerical order, …