Results 1 to 6 of 6

Thread: Store a list of objects

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Store a list of objects

    Hello,
    I have a list of objects of type myClass
    List list = <myClasse> new ArrayList <myClasse>();
    The objects of type myClass have different fields of which one of them is total. I would like to sort this list to be first in the list, the object which the total field is greatest. Shortly, I want to store a list of objects. Does any one has an idea about it. If you have one then please do post back. Thank you in advance
    Last edited by Miles Runner; 11-02-2010 at 03:56 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Store a list of objects

    Hello,
    Do you implement the interface "Similar" to your subject, and you defined in your method "compareTo" field is used to compare (and sort). This simple operation enables you to use sorting methods currently implemented in Java (SortedList, etc ...) This is the most simple and clean. Then, you can also rewrite by hand sorting method of your objects.

  3. #3
    Join Date
    Dec 2009
    Posts
    178

    Re: Store a list of objects

    Hello,
    I am trying the same code, but not successful. Here is the code
    Code:
    Public int compares(In o1, o2 Account) {
    		Double d1 = o1.getTotal();
    		Double d2 = o2.getTotal();
    		
    		if (d1> d2) {
    			return -1;
    		} else if (d1 == d2) {
    			return 0;
    		} else {
    			return 1;
    		}
    	}
    When I want to sort, i get
    Code:
    Collections.fate(listCompte)
    If you have a solution for this then please let me know.

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: Store a list of objects

    Hello,
    Actually I just noticed that it belongs to the package
    Code:
    java.lang
    And not the
    Code:
    non-java.util
    If you have any other alternative for doing the same that is performing the same task then please help me. Thanks in advance.

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Store a list of objects

    Hi,
    But you can also use a ComparatorThis can be very handy when you want to change the type of comparison. To do this simply use the method sort () appropriate:
    Code:
    Collections.fate(listCompte, tonComparator);
    Just try using the following code and see if it works for you. If not then post back with the query you have.

  6. #6
    Join Date
    Dec 2009
    Posts
    292

    Re: Store a list of objects

    Hello,
    I do not know how to use comparator. By implementing java.util.Comparator, I also have the method compare (Object o1, Object o2). In my case, I implement java.lang.Comparable and I compareTo ()
    Code:
    Public int compareTo(Account No.) {
    		Double d1 = o.getTotal();
    	    Double d2 = this.getTotal(); 
    	    if (d1> d2) {
    	    	return -1; 
    	    } else if (d1 == d2) {
    	    	return 0; 
    	    } else {
    	    	return 1; 
    	    }
    	}

Similar Threads

  1. Failed to retrieve stuff list in Ovi store through Nokia N8
    By Sanju!Ekta in forum Portable Devices
    Replies: 10
    Last Post: 26-09-2011, 12:02 AM
  2. Replies: 5
    Last Post: 01-03-2011, 10:14 AM
  3. Replies: 3
    Last Post: 30-10-2010, 10:40 PM
  4. Does Windows store a list of installed updates?
    By m3pilot in forum Windows XP Support
    Replies: 2
    Last Post: 20-06-2010, 02:35 AM
  5. cannot query for the list of Group Policy objects
    By Joe Nocella in forum Windows Server Help
    Replies: 2
    Last Post: 15-05-2008, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,300,305.90467 seconds with 16 queries