这些细节的实现应该去看一下接口Comparator吧。

解决方案 »

  1.   

    comparepublic int compare(Object o1,
                       Object o2)Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.Finally, the implementer must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals." 
    Parameters:
    o1 - the first object to be compared.
    o2 - the second object to be compared. 
    Returns:
    a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. 
    Throws: 
    ClassCastException - if the arguments' types prevent them from being compared by this Comparator.--------------------------------------------------------------------------------
      

  2.   

    interface Comparator定义了一个方法
    public int compare(Object o1, Object o2)
    所有implements Comparator必须override这个方法,
    该方法返回正数,0,负数分别代表o1 >, =, < o2//  如果obj2等于"Item 3",则obj1 < obj2
    if (((String)obj2).equals("Item 3")) {
        return -1;
    }//  否则返回obj1和obj2的字典顺序比较结果
    return ((String)obj1).compareTo((String)obj2);
    //  利用new NewComparator()生成的Comparator对象来排序TreeSet中的元素
    TreeSet treeset=new TreeSet(new NewComparator());整个程序的意思是将treeset中的"Item 3"字符串对象放在第一位(index为0),将其他字符串对象按字典顺序排序,然后将treeset中所有内容输出,输出结果为
    Item 3
    Item 0
    Item 1
    Item 2
    Item 4
    Item 5
    Item 6
      

  3.   

    意思是ITEM 3默认为最小的。
      

  4.   

    if(((String)obj2).equals("Item 3"))
        return -1;
    return ((String)obj1).compareTo((String)obj2);因为要保证把Item 3放在排序后的最前面,所以要特殊处理:
    第一句的意思是:强制使得所有其它字符串与"Item 3"比较后返回-1,也就是人为强制使得所有其它字符串都小于“Item 3”.从比较本身倒看不出算法思想,只是我觉得包含了“两两互相比较”的思想。
      

  5.   

    按排大小来看:
    if (((String)obj2).equals("Item 3")) {
        return -1;
    能保证把Item 3放在第一位
    return ((String)obj1).compareTo((String)obj2);
    是正常比较
    所以这段compare只是给出了2个Object的比较
    不涉及多个Object的排序
    如果对多个Object排序可能得用具体的排序方法来实现
    起泡,快速,都应该可以
      

  6.   

    我还是想不通,现在我不理解的地方主要是这一句:
    if (((String)obj2).equals("Item 3")) {
        return -1;
    只有obj2=“Item 3”,才会返回-1,怎么就说这一句是保证把Item 3放在第一位呢。
    谢谢大家的支持。
      

  7.   

    回复人: wanke() ( ) 信誉:100  2003-05-21 10:04:00  得分:0 
     
     
      我还是想不通,现在我不理解的地方主要是这一句:
    if (((String)obj2).equals("Item 3")) {
        return -1;
    只有obj2=“Item 3”,才会返回-1,怎么就说这一句是保证把Item 3放在第一位呢。
    谢谢大家的支持。
      
     
    你大概没学过排序的原理
    public int compare(Object obj1,Object obj2)用来判断2个obj的大小
    如果obj2和obj1比较,
    若obj2="Item 3"就返回-1
    否则就判断obj1和obj2的真实大小
    函数返回值为-1这就表示obj1<obj2.
    0表示obj1=obj2
    1就表示obj2<obj1
    根据这样的返回值的特点,若obj2="Item 3"就返回-1
    函数返回值为-1这就表示obj1<obj2.
    这样就保证了"Item 3"最大! 
      

  8.   

    既然是升序照你这么说"Item 3"应该在最后,但要求是把它放在最前这一句意思肯定是把它放在最前,
    if (((String)obj2).equals("Item 3")) {
        return -1;
    我不明白这一句为什么会成功把"Item 3"放在最前面,因为只有obj2="Item 3"才会这样,我就是不明白
      

  9.   

    public int compare(Object obj1,Object obj2)
    {
    if(((String)obj2).equals("Item 3"))
        return -1;
    return ((String)obj1).compareTo((String)obj2);
    }
    public int compare(Object obj1,Object obj2)用来判断2个obj的大小
    如果obj2和obj1比较,
    若obj2="Item 3"就返回-1
    否则就判断obj1和obj2的真实大小函数返回值为-1这就表示obj1<obj2.
    0表示obj1=obj2
    1就表示obj2<obj1
    当其他任何一个obj和"Item 3"比较的时候调用这个方法
    即i=compare(obj,"Item 3")
    根据函数的流程
    i的值为-1
    对于这个compare老说函数返回值为-1这就表示obj1<obj2
    即obj<"Item 3"
    这里的obj是任意的一个obj
    就是说任何的obj都小于"Item 3"
    这样就保证了"Item 3"最大!
    这回明白了????????????????
    如果还不明白,你再找一个叙述能力比较强的人把
    我只能说成这样了
      

  10.   

    很奇怪,我运行了一下程序,输出
    Item 0
    Item 1
    Item 2
    Item 3
    Item 4
    Item 5
    Item 6