Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().
class RecipeCompare implements Comparator<Recipe> {
@Override
public int compare(Recipe o1, Recipe o2) {
// write comparison logic here like below , it's just a sample
return o1.getID().compareTo(o2.getID());
}
}
Then use the Comparator as
Collections.sort(recipes,new RecipeCompare());
Answer from AllTooSir on Stack OverflowCollections.sort()
How does Collections.sort() organize files?
Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().
class RecipeCompare implements Comparator<Recipe> {
@Override
public int compare(Recipe o1, Recipe o2) {
// write comparison logic here like below , it's just a sample
return o1.getID().compareTo(o2.getID());
}
}
Then use the Comparator as
Collections.sort(recipes,new RecipeCompare());
The answer given by NINCOMPOOP can be made simpler using Lambda Expressions:
Collections.sort(recipes, (Recipe r1, Recipe r2) ->
r1.getID().compareTo(r2.getID()));
Also introduced after Java 8 is the comparator construction methods in the Comparator interface. Using these, one can further reduce this to 1:
recipes.sort(comparingInt(Recipe::getId));
1 Bloch, J. Effective Java (3rd Edition). 2018. Item 42, p. 194.
This comes very late, but there is a class in the JDK just for the purpose of having a sorted list. It is named (somewhat out of order with the other Sorted* interfaces) "java.util.PriorityQueue". It can sort either Comparable<?>s or using a Comparator.
The difference with a List sorted using Collections.sort(...) is that this will maintain a partial order at all times, with O(log(n)) insertion performance, by using a heap data structure, whereas inserting in a sorted ArrayList will be O(n) (i.e., using binary search and move).
However, unlike a List, PriorityQueue does not support indexed access (get(5)), the only way to access items in a heap is to take them out, one at a time (thus the name PriorityQueue).
TreeMap and TreeSet will give you an iteration over the contents in sorted order. Or you could use an ArrayList and use Collections.sort() to sort it. All those classes are in java.util
So I'm having a strange problem that I cant seem to understand how to fix.
I have written a program that reads names from a txt into an arraylist, and then I use Collections.sort() to put it in alphabetical order,
However, when I run it, Collections.sort() puts what should be the very first name last.
At first I thought, maybe there something weird about the unicode behind that name, so I removed it from the file, but then It did the same thing with the next name. Please help because I want to add a Binary search to this program. this is the file : https://drive.google.com/file/d/0B_ji2Hqu1ouAdDhHemFMal9iU2c/edit?usp=sharing
here is my code:
public static ArrayList<String> feNames = new ArrayList<String>();
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File("C:\\Users\\Austin\\Desktop\\female names\\female names.txt"));
while (s.hasNext()) {
feNames.add(s.next());
}
s.close();
Collections.sort(feNames);
for (String nam : feNames) {
System.out.println(nam);
}Thanks for the help!!
I’m writing a program and noticed that when using Collections.sort(), it wouldn’t organize files exactly as they’d be shown in the directory. Some files would be “misplaced” and this has been a pain in the ass since implementing a new function for my program would rely on how files are positioned. I’ve devised some potential workarounds but I’m curious how Collections.sort() works exactly