当前位置: 技术问答>java相关
如何排序??
来源: 互联网 发布时间:2015-08-23
本文导语: 有如下对象Node(1,"kevin"),Node(2,"henry"),Node(3,"jack"),Node(4,"apple") 我想将Node按名字排序输出,应该如何实现?? 排序后应该是Node(4,"apple"),Node(2,"henry").... 有什么好的方法?? | Obviously, all you need ...
有如下对象Node(1,"kevin"),Node(2,"henry"),Node(3,"jack"),Node(4,"apple")
我想将Node按名字排序输出,应该如何实现??
排序后应该是Node(4,"apple"),Node(2,"henry")....
有什么好的方法??
我想将Node按名字排序输出,应该如何实现??
排序后应该是Node(4,"apple"),Node(2,"henry")....
有什么好的方法??
|
Obviously, all you need is just a black-red tree. In Java, try:
---------------------------------------------
// In java.util package
public class TreeSet
extends AbstractSet
implements SortedSet, Cloneable, Serializable
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
---------------------------------------------
// In java.util package
public class TreeSet
extends AbstractSet
implements SortedSet, Cloneable, Serializable
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
|
很简单,利用Collections.sort方法,如下是我的程序段:
data是一个Vector,它的元素也是Vector.这个排序方法
是对data内部,根据data的元素的元素进行排序
Collections.sort(data,new Comparator()
{ public int compare(Object a,Object b)
{
String str1 = (String)((Vector)a).elementAt(i); String str2 = (String)((Vector)b).elementAt(i);
return str1.compareTo(str2);
} }
);
return data;
data是一个Vector,它的元素也是Vector.这个排序方法
是对data内部,根据data的元素的元素进行排序
Collections.sort(data,new Comparator()
{ public int compare(Object a,Object b)
{
String str1 = (String)((Vector)a).elementAt(i); String str2 = (String)((Vector)b).elementAt(i);
return str1.compareTo(str2);
} }
);
return data;
|
将Node对象全部放进一个数组
Node[] nodeArray = new Node[4];
...
Arrays.sort(nodeArray,new Comparator(){
public int compare(Object o1, Object o2) {
n1 = (Node)o1;
n2 = (Node)o2;
return n1.name.compareTo(n2.name);//你自己写,这里假设名字属性为name
}
});
Node[] nodeArray = new Node[4];
...
Arrays.sort(nodeArray,new Comparator(){
public int compare(Object o1, Object o2) {
n1 = (Node)o1;
n2 = (Node)o2;
return n1.name.compareTo(n2.name);//你自己写,这里假设名字属性为name
}
});
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。