aspose file tools*
The moose likes Beginning Java and the fly likes Array Objects - sorting? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Objects - sorting?" Watch "Array Objects - sorting?" New topic
Author

Array Objects - sorting?

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I have created an object which is stored in an array, is there a way to sort the array? After I add an entry into apps, I want to sort the array so that the array is in sequence by Software.

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Yes.

You can sort an array with your objects.

To do so your Object should implements the comparable interface and provide the implementation of the method compareTo else you can implement the Comparator interface and provide the implementation of the method compare.

If you are using later then you should pass the comparator as argument to the method sort in the java.util.Arrays class.

e.g. Arrays.sort(your object array) if your object implements comparable interface.

Arrays.sort(object array,comparator) if your object implements Comparator interface.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I tried that, I don't understand "comparators" so I defaulted to "null" (natural order), and I received a casting exception. Code is in the "addApplication" method.




Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

"Application" have no natural order. The "natural order" means the order you get by calling the Comparable.compareTo() method on the class, and Application doesn't implement Comparable; hence the ClassCastException.

If you want the Application objects to be sorted by the member "Software", which is what I think you've indicated, then you just need to add "implements Comparable<Application>" to your class declaration line, and add a compareTo method, like



This very common trick works because the String class implements Comparable already. The result of comparing two Applications is now the result of comparing their "software" members.

[Jess in Action][AskingGoodQuestions]
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Natural orders only work for the objects that implements the java.lang.Comparable interface.

In your case java would have no idea how to sort your object?

based on software,version,serialno? No it has no idea how to sort user object unless you tell java how want to sort your object using Comparable interface.

lets say you want to sort your object nbased on serial version then the code would be like this.



compareTo return 0,1,-1 based on the comparison.

See the API for comparable interface.

Since String,Wrapper classes already implemented Comparable interface you can directly use them to sort but your own class does not implement the Comparable interface,so you have to tell java how do you want your object to be sorted out. in this case using version no.
hope it helps you.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Yes! I got it working Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Objects - sorting?