Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting ArrayList

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please let me know how to sort the data in the below array list.




I know that we can put A4.get(0) and A4.get(1) in TreeSet and sort the list, but can the list be sorted just using above code like,

I used Collections.sort(A4) it is giving wrong results. Also, while using Collections.sort(A4), is the list sorted by taking A4.get(0) as a reference or A4.get(1) as reference.

Please let me know>
 
Campbell Ritchie
Marshal
Posts: 53110
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you getting? How are you sorting? Remember Collections#sort takes Comparable as its parameter, and I don't think Lists are usually Comparable. Strings are however, so you should be able to sort a List<String>.
Remember the order Strings are sorted in: ASCIIbetical.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!