DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Sort Vectors Of Objects In Java

04.01.2006
| 13953 views |
  • submit to reddit
        Your object must implement the interface Comparable

For example:

public class Link implements Comparable {

	String id_link = "";

	public int compareTo(java.lang.Object o) {
		
	        //Here you can put your code for compare two objects. Something like this:

	        Link tmp = (Link) o;
	        
	        return this.getId_link().compareTo(tmp.getId_link());
	} 
}

Sorting the Vector:

		java.util.Collections.sort(myVector);

Enjoy!

Edu