Results 1 to 3 of 3
  1. #1
    drymsza1234 is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Sorting ArrayList by object data


    So I have an ArrayList of objects which have information to them like name and a few int values. I wanted to get the top 10 objects of a certain int value. How would I go about doing that?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Rep Power


    Java Code:
        List<YourClass> yourList;
        Collections.sort (yourList, new Comparator<YourClass>() {
          public int compare(YourClass o1, YourClass o2) {
            return o1.getSomeIntValue () - o2.getSomeIntValue ();

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    This is easy if the objects held by the ArrayList implement the Comparable interface. If so, simply call java.util.Arrays.sort(myArrayList), and pick out the top 10. If the objects don't implement Comparable, you'll could do the same but sort with a Comparator object. Google for a tutorial and give it a try.

    Much luck!

Similar Threads

  1. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 09:27 PM
  2. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  3. Sorting an ArrayList
    By flesh-bound-book in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 12:20 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts