Results 1 to 4 of 4
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default sorting objects in java

    Hi is it possible to sort objects in java by a particular attibute. For eg. I have several 'user' objects which have an attribute say empno which is a numeric. Is it possible to sort all the user objects in ascending order by emp no. If yes, then how,

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    What have you tried? How are the Objects stored? In a List? In an array? As separate variables?

    You could write a sorting method yourself. Or you could implement Comparable. Or you could set up a Comparator. Google and the API are your friends.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    I agree with Kevin. You should read up on how to implement the Comparable interface in your class. Please see link to API for Comparable: Comparable (Java 2 Platform SE v1.4.2)

    Best,
    --user0--

  4. #4
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    thanks! the objects are stored in a arraylist.
    Last edited by nn12; 01-08-2011 at 10:44 AM.

Similar Threads

  1. Replies: 2
    Last Post: 12-13-2010, 11:12 AM
  2. Java - Comparator, sorting arrayLists
    By Kerrai in forum New To Java
    Replies: 15
    Last Post: 10-24-2010, 09:41 PM
  3. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  4. Sorting/Searching Objects with multiple types.
    By gcampton in forum New To Java
    Replies: 20
    Last Post: 10-21-2009, 11:58 PM
  5. Sorting strings in java?
    By alexander.s in forum New To Java
    Replies: 10
    Last Post: 09-01-2008, 04:14 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
  •