• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Objects - sorting?

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! I got it working Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic