• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java sorting based on list size

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to sort my arrayList based on it's size, I have a users array list which contains a friends arrayList, and I am trying to sort the users list based on how many friends they have (most friends to least) I am unable to find a single example of how this is to be done online, any help is much appreciated! I will be back shortly to reply
 
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is this how you are storing your data?



or did you mean that you have a Users object which contains an ArrayList of friends?

-Zach
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Facebook class as my driver class which contains an arrayList of FacebookUsers


I have a facebookUser class which contains the arrayList of friends


 
Zach Rode
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most elegant method I can think of would be using the overloaded version of Collections.sort(List<T> list, Comparator<? super T> c).

The Comparators section of this Java doc might be useful to you Object Ordering

-Zach
 
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider implementing Comparable interface for FacebookUser class. Compare friends list size in compareTo() method.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:Consider implementing Comparable interface for FacebookUser class. Compare friends list size in compareTo() method.



Zach's suggestion to use a Comparator is the better way to go, in my opinion. The semantics of Comparable are narrower and are better suited for comparing things based on more intrinsic attributes. A Comparator has much looser semantics and is better suited for more arbitrary criteria. @OP wants to order the users according to the number of friends they have. This fits better with my idea of what a Comparator would be for.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, does anyone know of a tutorial that explains how to do this? Every one I have come accrossed thus far is complete garbage, and explains nothing in detail on how it is to be done.
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would the compareTo method override look like? I don't know how to write the compareTo method.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zach and I suggest using Comparable Comparator instead of compareTo()

Try searching for information on how to implement Comparable?

Edit: Corrected typo. Thanks to Zach and Norm for pointing out my mistake.
 
Zach Rode
Rancher
Posts: 89
13
Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,

Do you mean comparator? I thought you said not to implement comparable.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zach and I suggest using Comparable instead of compareTo()


Is that a typo?  Earlier you were promoting using Comparators.  Comparators can be external to the class and allow a list of objects to have different Comparators for each property in the class that you want to sort on.
Implementing Comparable restricts the comparisons to a single property unless there is tricky code to change what is compared depending on some flag.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Is that a typo?


Yes, it was. Thanks, I corrected it.

Comparators are external to the file and allow a list of objects to have different Comparators for each property in the class that you want to sort on.


I think you have the right idea, only I'd probably say it differently. Comparators aren't restricted to a single property either, although having more than one criteria might make the sorting unintuitive. It depends on the mix.

Comparators, as implied by the tutorial that Zach cited, give us a way to sort objects in an order other than their natural ordering.

So you can make Comparators that order users by number of friends, frequency of posts they make, number of followers, their geographical location, etc.  If you follow the example given in that tutorial, you might end up with something like this:

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is an fine case for a static method in the Comparator interface (Comparator.comparingInt(function that maps a facebookuser to the size of its friends list), eventually followed by .thenComparing(perhaps on name?). Do have a look, it makes for elegant and short code.

But implementing this as a Comparable<FaceBookUser> does not look unlogical: like LinkedIn, it sometimes seems that it is all about who has the most connections/followers...
 
kennith stomps
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still have gained no further insight from any of these answers. It is as if no one here is really allowed to answer a question, only beat around the bush.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Collections sort method takes an instance of a Comparator class to use when deciding how to order items in a list.  The Comparator interface defines one method that must be defined: compareTo.  The compareTo method takes two instances of a class, uses some logic to determine the order of the two instances and Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
For numeric properties, subtraction will return the order.  Say the values to compare are 4 and 6.  4-6 is < 0 meaning the object with the 4 goes before the object with the 6.  If the values were 5 and 3,  5-3 > 0 ,meaning the 5 goes after the 3.
The Collections sort method will call the compareTo method, pass it two objects and expect an int value to be returned that determines what order the two objects should go in.


Do you know how to write a simple class that implements an interface?  If so define a class that implements the Comparator interface.  It will need to define a method: compareTo that takes two objects/
Inside the method, add some statement(s) that return an int value as discussed above.

See the post at Today 3:29:47 PM  for a sample skeleton of the class.  See lines 3-7 or 9-13.  The total code is more than is needed.
Also do a Search here or with Google for code samples.

Create an instance of that class and pass it as the second arg to the sort method:
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kennith stomps wrote:I still have gained no further insight from any of these answers. It is as if no one here is really allowed to answer a question, only beat around the bush


You got plenty of useful answers, in my opinion. And we're not beating around the bush. We just don't give out code that we think you should be able to figure out from the information and feedback you've been given.

Remember, the Ranch is not a code mill so a bit of effort on your part would go a lot further than comments like what you just made.
 
Omkar Shetkar
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Omkar Shetkar wrote:Consider implementing Comparable interface for FacebookUser class. Compare friends list size in compareTo() method.



Zach's suggestion to use a Comparator is the better way to go, in my opinion. The semantics of Comparable are narrower and are better suited for comparing things based on more intrinsic attributes. A Comparator has much looser semantics and is better suited for more arbitrary criteria. @OP wants to order the users according to the number of friends they have. This fits better with my idea of what a Comparator would be for.



Totally agree. Comparator is the way to go when you need flexibility of property to use for ordering.
In this particular context, Comparator most suited.
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kennith, you can easily create a comparator to pass to the sort() method by calling the factory methods on the Comparator interface, and passing a lambda expression or method handle that describes the property that you want to compare by.

In your particular case, you want to sort a list of users by the size of something. That means you would use the Comparator.comparingInt() method. You pass it a function that given a user, returns what int property of that user should be used in the comparison. What property? The size of the user's friend list.

I suggest you write some code to get this done, and if you're stuck on it, you post it here and we can comment on it.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:you can easily create a comparator to pass to the sort() method by calling the factory methods on the Comparator interface, and passing a lambda expression or method handle that describes the property that you want to compare by.


Excellent suggestion. Have a cow for that nugget of new knowledge you gave me today.

Doing that, the code I proposed before might look something like this:

 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:It is an fine case for a static method in the Comparator interface (Comparator.comparingInt(function that maps a facebookuser to the size of its friends list), eventually followed by .thenComparing(perhaps on name?). Do have a look, it makes for elegant and short code.



OP wrote:I still have gained no further insight from any of these answers. It is as if no one here is really allowed to answer a question, only beat around the bush.

 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu, I'd keep those comparators with the class they're comparing though. I really don't like classes that just act as a constant container.

A declaration would then be FacebookUser.BY_POST_COUNT_ORDER and when using a static import you can call it like this:
I find this way rather elegant.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't notice there was an earlier suggestion, so in all fairness, @Piet deserves a cow, too!

@Stephan, I tend to agree with you on the constant containers class. I'm not sure if I'm influenced by the example in the tutorial or if my intuition tells me to keep non-natural order sort criteria away from the domain class. I thought it was the latter. My intuition has proven to be wrong sometimes though.

@OP, take Stephan's advice to keep the comparator constant in the FacebookUser class. However, if you find that you have to add more comparators, I think a separate "FacebookUserSort" class might be justified.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I didn't notice there was an earlier suggestion, so in all fairness, @Piet deserves a cow, too!


Now I'm a bit embarrassed! My intention was to point out that OP probably is unfamiliar with Comparables and Comparators. I hoped to have OP delve a little bit into the API's of these, so that he could try out for himself.
I hope to hear if he succeeded in sorting the list as planned!

But thanks for the cow. I just gave you a cow too, as a result of pressing the wrong button (should have been 'quote'). But deserved anyway, so enjoy!      
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic