Tree set for sorting

code:

   Set<Pattern> sortedPatterns = new TreeSet<Pattern>();
   for(Pattern pattern : project.getPatterns()){
    sortedPatterns.add(pattern);
   }
   project.setPatterns(sortedPatterns);

 

Then the Pattern need to implements Comparable interface

then finished your method compareTo with your requirement is ok

here is the example:

 

	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Pattern pp1 = (Pattern)o;
		Pattern pp2 = this;
		if(pp2.getType()==null || pp2.getType().equals("")){
			return -1;
		}
		if(pp1.getType()==null || pp1.getType().equals("")){
			return 1;
		}
		return (pp2.getId()).compareTo(pp1.getId());
	}

 

你可能感兴趣的:(tree)