I'm having a bit of trouble bubblesorting Calendar
s by date, I'm not sure what's wrong with my code.
First: I have a class(named Note
) which contains multiple variables and methods, one of those variables is a Calendar
.
Then I have an ArrayList<aboveclass>
named list
containing multiple instances of the above class. I'm trying to sort list
by the date of each class instance.
This is my code:
for(int i = 0; i<list.size(); i++){
for(int x=0; x < list.size() - x - 1; x++){
if(list.get(x).date.after(list.get(x+1).date)){
Note temp = list.get(x);
list.set(x, list.get(x+1));
list.set(x+1, temp);
System.out.println(i + " and " + x + " Switched");
}
}
}
Nothing is being sorted though, and that System.out.println
is never going. I have also tried switching .after
with .before
with no differences.
Is there something I'm missing?
Thanks
If you do not need to sort your list by a bubble sort, then you can use java.util.Collection.sort()
.
Your code will become :
Collections.sort(list);
You need however to make Your class (Note
if I am right) implement Comparable or to create a comparator.
Here you can find some examples.