• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort Array List based on boolean  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array list of ProvidedItems objects. One of the fields in the ProvidedItems is a boolean / Boolean isPrimary().
I need to sort my list so that if isPrimary = true, it always comes back as the first element.

Not sure how to do this.

Thanks.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the ProvidedItems class implements the Comparable interface you can just do:

 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if not, use a Comparator<ProvidedItems>.

Either which way you go, the basic structure is as follows:
 
Joe White
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:And if not, use a Comparator<ProvidedItems>.

Either which way you go, the basic structure is as follows:


Thanks Rob, this is more along the lines of what I was looking for.

JOe
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!