class Dog{
private String name;
public Dog(String name){
this.name = name;
}
String name;
}
public class Test{
public static void main(){
LinkedList list = new LinkedList();
list.add(new Dog("baaa"));
list.add(new Dog("caaa"));
list.add(new Dog("aaaa"));
}
}
private String name;
public Dog(String name){
this.name = name;
}
String name;
}
public class Test{
public static void main(){
LinkedList list = new LinkedList();
list.add(new Dog("baaa"));
list.add(new Dog("caaa"));
list.add(new Dog("aaaa"));
}
}
LinkedList<Dog> dogs=..;
for(int i=0;i<dogs.size-1;i++)//排序
for(int j=i+1;j<dogs.size();j++)
{
Dog d1=dogs.get(i);
Dog d2=dogs.get(j);
if(d1.compareTo(d2)>0)
{
dogs.set(j,d1);
dogs.set(i,d2);
}
}
class Dog implement Comparable
{
....//原省略
public int compareTo(Object o)
{
Dog o1=(Dog)o;
return this.name.compareToIgnoreCase(o1.name);
}
}
Collections.sort(fileList, new FileNodeComparator(form.getSortName(),form.isAsc()));
2,实现一个自定义的Comparator
class FileNodeComparator implements Comparator{
private String sortName = "name";
private boolean isAsc = true;
FileNodeComparator(String sortName,boolean isAsc){
this.sortName = sortName;
this.isAsc = isAsc;
}
public int compare(Object arg0, Object arg1) {
。。
。。
。。
。。
}
}
如果只需要判断重复则只需要HashSet,覆盖equals和hashCode方法怎么排序是你Comparator的事情,你只要正确的实现
compare(Object o1, Object o2)方法就可以,至于你认为哪个在前哪个在后,需要按什么排序都是你自己的事情,TreeSet根本不需要知道
class Dog{
String name;
String age;
}
并且实现了两个方法CompName(),CompAge(),对过传入参数,可以实现对于不同属性的比较进行排序
std::sort(m_Data.begin(), m_Data.end(), Dog::CompName);
std::sort(m_Data.begin(), m_Data.end(), Dog::CompAge);那对应到JAVA中,可以用什么方式实现呢......