Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Sorting an ArrayList  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have an ArrayList I need to sort. I know i can use the sort() method.
But the ArrayList holds objects with multiple attributes. Attributes I need to sort are of type LocalTime and LocalDate.
I first need to sort the list on LocalDate then LocalTime.

Can someone point me in the good direction?

thanks
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juan Bauer wrote:Can someone point me in the good direction?


Yes. Create a Comparator to sort your objects in whatever way you want, and then use Collections.sort(Comparator).

Hope it helps.

Winston
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that helps, don't have it working though;)

Can you see why I get this error?

Exception in thread "main" java.lang.AbstractMethodError: agenda.AfspraakPeriodiek.compareTo(Ljava/lang/Object;)I
at java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.base/java.util.Arrays.sort(Arrays.java:1314)
at java.base/java.util.Arrays.sort(Arrays.java:1508)
at java.base/java.util.ArrayList.sort(ArrayList.java:1585)
at java.base/java.util.Collections.sort(Collections.java:142)
at agenda1/agenda.AfspraakSorteer.sorteerOpBegindatum(AfspraakSorteer.java:16)
at agenda1/agenda.Agenda.geefAfspraken(Agenda.java:166)
at agenda1/agenda.AgendaMain.main(AgendaMain.java:43)



 
Marshal
Posts: 6588
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Please show us where you implemented comparators Winston suggested you? Also I don't see you passed them to Collections.sort(...) method.
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@OP

Please show us where you implemented comparators Winston suggested you? Also I don't see you passed them to Collections.sort(...) method.



here:
 
Rancher
Posts: 3931
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've not implemented the compareTo required by the Comparable<Afsprak> interface.
That's why you get that error.
 
Liutauras Vilda
Marshal
Posts: 6588
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juan Bauer wrote:I first need to sort the list on LocalDate then LocalTime.


That doesn't seem to be what you specified as an requirement.

Research the difference between the Comparable and Comparator interfaces.

Check how using Java 8 you could implement them in fairly concise way.
 
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely your parameter won't be type LocalTime? I think you should create a Comparator<Afspraak>, rather than making Afspraak implekent Comparable. But before doing anything, read this part of the Java™ Tutorials, and the links therein to Comparable and Comparator. After that you will know that Comparable objects are naturally ordered by a criterion, so I believe LocalDate and LocalTime will both implement Comparable. But an Afspraak doesn't have a criterion you can sort it by, but multiple criteria. In which case I think it is better to create a Comparator<Afspraak>, as Winston has already told you. This is the old way to do that sort of thing; if you look in the Java™ Tutorials again, you will find you can write a λ to shorten that code no end:-I have concocted a field naam which also implements Comparable<Naam>. I am sure you can alter that to use the beginTijd field instead, and you can also follow the examples in the first Java™ Tutorials link I showed you.
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Surely your parameter won't be type LocalTime? I think you should create a Comparator<Afspraak>, rather than making Afspraak implekent Comparable. But before doing anything, read this part of the Java™ Tutorials, and the links therein to Comparable and Comparator. After that you will know that Comparable objects are naturally ordered by a criterion, so I believe LocalDate and LocalTime will both implement Comparable. But an Afspraak doesn't have a criterion you can sort it by, but multiple criteria. In which case I think it is better to create a Comparator<Afspraak>, as Winston has already told you. This is the old way to do that sort of thing; if you look in the Java™ Tutorials again, you will find you can write a λ to shorten that code no end:-I have concocted a field naam which also implements Comparable<Naam>. I am sure you can alter that to use the beginTijd field instead, and you can also follow the examples in the first Java™ Tutorials link I showed you.



Alright got the first sort working!

But can you help me with the second filter?
First everything should be sorted on LocalDate date then by LocalTime time


 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juan Bauer wrote:. . . Alright got the first sort working!

Well done
But why isn't the datum field private? All fields should be private in normal programming, except those sued as global constants. Why aren't you using the getDatum() method?...or write a λ:-There is an even shorter way to write that, but I shall keep quiet about that for the moment.

. . . First everything should be sorted on LocalDate date then by LocalTime time . . .

That sounds rather like an example from the first Java™ Tutorials link I gave you.
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Juan Bauer wrote:. . . Alright got the first sort working!

Well done
But why isn't the datum field private? All fields should be private in normal programming, except those sued as global constants.



Good one!, I now added the getters and setters and made all attributes private.
I made them protected because Afspraak is a superclass.

Don't get the final solution of the filtering yet, but will go to the tutorials again.

thanks!
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot figure it out.

I made a second Comparator Class for the second filter Tijd.

but when I sort it just overwrite the first sort. :S






 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juan Bauer wrote:. . . I made a second Comparator Class for the second filter Tijd. . . .[code=java:firtline[11]]return o1.getBegintijd().compareTo(o2.getBegintijd());[/code]

What's wrong with that? I think that looks correct.

I think method that should sorteerAfspraken() (=sort appointments, I hope), and should have void rather than a return type because you are sorting the same List as provided as the parameter. If you are going to return a List, make that a copy:-That way you take a copy of the List to sort. I think you will find those sort methods are 1368 in the most dubious classification of methods ever seen: here, in which case it can probably be made static.
The following isn't Java®, but I think it will give you the logic for a Comparator:-
 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:.... . .

You can generailse that code to this, which will take any type of List:-
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for all your help!

I now created this code:



When I run it I get a error:

The abstract class Afspraak is a super class for 2 subclasses.


Any suggestions? I hope i'm finally near the solution!
 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to see you have found comparing() and thenComparing()

You can shorten that code, I think:-...but it will look better formatted like this:-You might do better to call the Comparator byDateAndTime or sorteerDatumEnTijd.
I can see nothing in the code you posted that could cause that exception to be thrown; please supply more details.
 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . .  can see nothing in the code you posted that could cause that exception to be thrown . . .

. . . but now I have looked properly and seen you have the sorted() call in line 3. Again, let's have some shortening and formatting:-The exception has now moved to line 4, and the reason is that you didn't supply a suitable Comparator as an argument to that method.
 
Juan Bauer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to look at my formatting, makes it much better readable!

Still not there however.

If I run this code it gives an error. It suggest to make a cast (Afspraak). If I do that it says that I need to change to Comparator<Object>, also not working.




I read on a forum that in order to call an method to get the attribute the code should look like this:


Now the code is compiling but there is no sorting happening.
Do I maybe need to create my own comparing method?
 
Campbell Ritchie
Marshal
Posts: 63427
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Afspraak::getBegintijd is a correct method reference, but:-
  • 1: Your current problem is getting the sorting working; you don't need to change your λs to method references to do any sorting.
  • 2: The more places you ask and the more discussions you start, the more risk there is of getting confused by different opinions. Stick to one forum for the time being, and we're the best
  • 3: Do some debugging; get two Afspraken and print their difference from compare(). See what is happening in the List.
  • 4: Post all your code as it is at the moment, so we can see what is happening and try the code ourselves. Better still, format it correctly. You seem to be using an IDE; that will do the formatting for you.
  • 5: As for the last exception, look at this FAQ.
  •  
    Juan Bauer
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alright, that helps. Lots to learn!

    Can I maybe send you all the code in a pm? Its a school assignment and i'm not sure if it is appreciated if I post everything publicly. Ofcourse I can post the relevent classes and hopefully the solution.

    Let me know if thats ok
     
    Campbell Ritchie
    Marshal
    Posts: 63427
    205
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Juan Bauer wrote:. . . Can I maybe send you all the code in a pm? Its a school assignment and i'm not sure if it is appreciated if I post everything publicly. . . .

    It would look even worse if you were caught PM‑ing people, I am afraid. I think you will have to do some debugging, starting with what I suggested first.
    Run all your Stream code together into a single statement, as I showed you earlier today, and print out the List to see what is happening. I can't see why you aren't getting the Afspraken list sorted.
    Don't change the λs to method references until after you have got the sorting problem corrected.
    Inset a line 3½Remember that Streams run on lazy execution; nothing will happen until the terminall operation (here=.collect(Collectors.toList());) runs.
     
    Juan Bauer
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Damn this was frustrating but I figured it out!

    I had nothing to do with the sorting, that worked fine. Your direction to create test cases made me realise the sorting did what it had to.
    The problem was in a while statement I created. Not all cases where added to the list. Because I expected a different result I did not look close enough at the order.

    Thank you so much for your help Campbell Ritchie! learned alot

    cheers!
     
    Campbell Ritchie
    Marshal
    Posts: 63427
    205
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done correcting your problem

    Juan Bauer wrote:. . . Thank you . . .

    That's a pleasure
     
    Destiny's powerful hand has made the bed of my future. And this tiny ad:
    Become a Java guru with IntelliJ IDEA
    https://www.jetbrains.com/idea/
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!