在java中compareTo方法返回什么

在java中compareTo方法返回什么,java,comparator,compareto,Java,Comparator,Compareto,我有一个代码片段,它在数组中查找中间值,然后根据与中间值的接近程度(从最小到最大)对数组进行排序,但我不明白这些东西是如何工作的 public static Integer[] sort(Integer[] array) { Arrays.sort(array); final double median; if (array.length % 2 == 0) median = ((double)array[arra

我有一个代码片段,它在数组中查找中间值,然后根据与中间值的接近程度(从最小到最大)对数组进行排序,但我不明白这些东西是如何工作的

 public static Integer[] sort(Integer[] array)
    {
        Arrays.sort(array);
        final double median;
        if (array.length % 2 == 0)
            median = ((double)array[array.length/2-1]+ (double)array[array.length/2])/2;
        else
            median =  array [array.length/2];

        Comparator<Integer> compareToMedian= new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2)
            {
                double value = Math.abs(o1 - median) - Math.abs(o2 - median);
                if (value == 0)
                    value = o1 - o2;
                return (int)value;
            }
        };

        Arrays.sort(array, compareToMedian);
        return array;
    }
公共静态整数[]排序(整数[]数组)
{
数组。排序(数组);
最终双中位数;
if(array.length%2==0)
中值=((双)数组[array.length/2-1]+(双)数组[array.length/2])/2;
其他的
中位数=数组[数组长度/2];
比较器比较器中值=新比较器()
{
@凌驾
公共整数比较(整数o1,整数o2)
{
双值=数学abs(o1-中值)-数学abs(o2-中值);
如果(值==0)
数值=o1-o2;
返回(int)值;
}
};
排序(数组,比较中值);
返回数组;
}
我真正感到困惑的是,在这种特殊情况下,值到底意味着什么。它只是从最小到最大排序结果吗?就像我的
结果
在一种情况下等于-5,在另一种情况下等于-2,最后变成-5,-2,3?并对数组进行排序?

compareTo()返回一个int以显示哪个值更大 如果两者相等,则为0

+如果第一个值更大,则为ve

-如果第二个值更大,则为ve


compareTo(5,5)返回0

compareTo(6,5)返回任何正值

compareTo(5,6)返回任何负数

//    going line by line for explanation

    public static Integer[] sort(Integer[] array)
    {
        Arrays.sort(array);//first of all sort the numbers in increasing order
        final double median;
        /*
        now in sorted numbers median is the
        --- average of middle two values for even count of numbers; like for 10 numbers median is (4th item +5th item )/2
        --- middle value if there are odd count of numbers like for 11 items the median is the 6th item
         */
        if (array.length % 2 == 0)
            median = ((double)array[array.length/2-1]+ (double)array[array.length/2])/2;
        else
            median =  array [array.length/2];
        //now we have the median



//        here we have a Comparator for comparing any value during with the median value
        Comparator<Integer> compareToMedian= new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2)
            {
//                first we check the distance of two numbers from the median; that is the difference from the median
                double value = Math.abs(o1 - median) - Math.abs(o2 - median);
                if (value == 0)//if the difference is same then we compare the numbers
                    value = o1 - o2;
                return (int)value;//otherwise we return the difference
            }
        };

        Arrays.sort(array, compareToMedian);//sort the numbers with respect to median
        return array;
    }
//逐行解释
公共静态整数[]排序(整数[]数组)
{
Arrays.sort(array);//首先按递增顺序对数字进行排序
最终双中位数;
/*
现在在排序的数字中,中位数是
---偶数计数的中间两个值的平均值;如10个数字的中值为(第4项+第5项)/2
---中间值如果有奇数个数字,如11项,中位数为第6项
*/
if(array.length%2==0)
中值=((双)数组[array.length/2-1]+(双)数组[array.length/2])/2;
其他的
中位数=数组[数组长度/2];
//现在我们有了中位数
//在这里,我们有一个比较器,用于比较过程中的任何值与中值
比较器比较器中值=新比较器()
{
@凌驾
公共整数比较(整数o1,整数o2)
{
//首先,我们检查两个数字与中位数的距离,即与中位数的差值
双值=数学abs(o1-中值)-数学abs(o2-中值);
如果(value==0)//如果差值相同,那么我们比较数字
数值=o1-o2;
return(int)value;//否则返回差值
}
};
Arrays.sort(array,compareTomidian);//根据中位数对数字进行排序
返回数组;
}

您是否阅读了Comparator.compare的文档?在调试此代码方面,您尝试了什么?你的“像我一样”这句话目前对我来说没有多大意义,特别是当
结果
在代码中的任何地方都没有出现时…:(::(:(:)你是在问返回值的意思是什么,还是在问数学
math.abs(o1-中值)-math.abs(o2-中值)
有效?另请参阅。我问排序基于什么。返回值还是什么?如果是,它是如何工作的?我想你的意思是
比较
,而不是
比较
比较器
比较
,这就是你代码中的内容。
比较
比较
。非常感谢,这对我的帮助很大虽然如此,但我仍然想知道返回值对数组顺序的影响。它只是使用冒泡排序或其他方法检查数组中返回值的每个值吗?使用比较器给出的规则(在compareTo方法中)比较数组中的两个项,直到结束.Further array.sort不是冒泡排序,而是更快的排序技术。@javafan谢谢,顺便说一句Quadgen网站不错。希望我把代码讲清楚了。