package com.vector;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Main {
public static void main(String[] args){
TreeSet ts =new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int result= ((String)o1).length()-((String)o2).length()==0?((String) o1).compareTo((String) o2):((String)o1).length()-((String)o2).length();
return result;
}
});
ts.add("abc");
ts.add("fwc");
ts.add("acwq");
ts.add("sdg");
ts.add("agadgv");
Iterator it = ts.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
package com.vector;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Comp> ts = new TreeSet<>(new Comp());
Comp t1 = new Comp("asc");
Comp t2 = new Comp("afc");
Comp t3 = new Comp("sadfas");
ts.add(t1);
ts.add(t2);
ts.add(t3);
System.out.println(ts);
}
}
class Comp implements Comparator<Comp>{
String o;
public Comp(){
}
public Comp(String o){
this.o= o;
}
@Override
public String toString() {
return "Comp{" +
"o='" + o + '\'' +
'}';
}
@Override
public int compare(Comp o1, Comp o2) {
int result =o1.o.length()-o2.o.length()==0?o1.o.compareTo(o2.o):o1.o.length()-o2.o.length();
return result;
}
}