Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort ArrayList  RSS feed

 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How we sort ArrayList.
If we sorre 20 car object in a ArrayList and we want to sort care object according to speed.How we can do that.

Regards,

Shrawan
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked out the API for the Collections class? There are some useful methods there.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Collections.sort() to sort an ArrayList.
You will either need your class to implement the Comparable interface and provide a suitable compareTo method, or you will need to create a Comparator class that implements a suitable compare method and pass this to the Collections.sort() method.

See the Java sorting tutorial and the API doc for each of the classes for more details.
[ September 28, 2006: Message edited by: Joanne Neal ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!