LinkedList中将对象按照某一属性排序

例如,链表 treelist 声明如下:

LinkedList<TreeNode> treelist = new LinkedList<TreeNode>();

其中 TreeNode 是自己定义的一个类,类中有一个 int 类型的 value 属性,若要以 TreeNode 中的 value 属性排序,可以如下实现:

        1 、添加一个类,实现 Comparator 接口

public class TreeNodeComparator implements Comparator<TreeNode> {

 

  @Override

  public int compare(TreeNode o1, TreeNode o2 ) {

          return (o2 . value -o1. value ); ///?????

  }

}

2 、在要排序的地方,只需这样的一句话即可:

Collections.sort (treelist, new TreeNodeComparator()); // 排序

这样就实现了对treelist中的对象用value属性从非递减排序

原文:http://blog.csdn.net/lgjfly/article/details/5569615

原文地址:https://www.cnblogs.com/azhqiang/p/6710202.html