Results 1 to 6 of 6

Thread: Sort a table for several classes

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Sort a table for several classes

    Hello,
    I have a question for you guys
    Given that:
    - I have two classes A and B, which implements C.
    - I have my List of object C.
    - I would classify: the first B then the A's.
    How this is possible, I need an explanation to this. If you know this or you can explain me this then it will be great.

  2. #2
    Join Date
    Dec 2009
    Posts
    263

    Re: Sort a table for several classes

    Hey,
    I am new to this concept but I think I have a code it will make understand you the concept, that what exactly happens here.
    Code:
     
    Public interface C {
     
    }
     
    Public class Has {
     
    }
     
    Public class B {
       List <C> list
    }
     
    Public class D {
       private B root;
    }
    I hope I am correct here.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Sort a table for several classes

    Hello,
    The Class class provides a method to find the class name of an object: Class.getName (). And to find the class of an object, use the method getClass () of the Object class.
    In your case,
    Code:
    int i; / / position in the ls
    if(ls.get(i).getClass().getName() == "package.A"){
         / / class A shares for
    }else if(ls.get(i).getClass().getName() == "package.B"){
         / / Class B shares for
    }
    Then for sorting, you can for example create two tables, one contains the objects A, the other objects B. By browsing through the list you store objects in the appropriate table. After that, you have to empty the list and fill in the table object B first, then with the table of objects A.

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Sort a table for several classes

    Hello,
    Look at the following line in your statement, I think you can replace this with a better method than this
    Code:
    if(ls.get(i).getClass().getName() == "package.A")
    And now look at this method, I feel this is better and much more productive than the above.
    Code:
    if(ls.get(i).getClass() == A.class)
    Hope this will help you and you understand the concept behind this.

  5. #5
    Join Date
    Nov 2009
    Posts
    347

    Re: Sort a table for several classes

    Hello,
    For your problem requires that C implements Comparable to use the lot later, it must be a class not an interface. Then A and B must inherit from C instead of implementing the coup. In your method compareTo in C returns that you think should be that your objects are Class A or B. I think this should solve your problem and your program should compile successfully.

  6. #6
    Join Date
    Nov 2009
    Posts
    359

    Re: Sort a table for several classes

    Hello,
    I have modified your code, Have a look at it.
    Code:
    int i; / / position in the ls
    if(ls.get(i).getClass().getnm() == "pack.A"){
         / / class A shares for
    }else if(ls.get(i).getClass().getnm() == "pack.B"){
         / / Class B shares for
    }

Similar Threads

  1. How to sort multiple columns in Excel Pivot Table
    By Nicoloid in forum MS Office Support
    Replies: 2
    Last Post: 17-02-2012, 08:01 PM
  2. Replies: 2
    Last Post: 24-01-2012, 02:07 PM
  3. Automatic sort and update league table in Excel
    By Raju Chacha in forum Windows Software
    Replies: 3
    Last Post: 08-01-2012, 04:18 PM
  4. Sort the elements of a table in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 06-03-2010, 11:59 AM
  5. Table and abstract classes
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 10:49 AM

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,750,616,391.19420 seconds with 16 queries