有什么简单的方法来降序排序一个数组,就像他们有一个升序排序在数组类?

还是说我必须停止懒惰,自己做这件事:[


当前回答

我不知道你的用例是什么,但是除了这里的其他答案之外,另一个(惰性)选项是仍然按照你指出的升序排序,但然后以反向顺序迭代。

其他回答

不能直接使用Arrays.sort()和Collections.reverseOrder()对原语数组(即int[] arr ={1,2,3};)进行反向排序,因为这些方法需要引用类型(Integer)而不是原语类型(int)。

但是,我们可以使用Java 8 Stream首先对数组进行装箱,以倒序排序:

// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};

// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
    .sorted(Collections.reverseOrder())
    .mapToInt(Integer::intValue)
    .toArray();

System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]

另一个解决方案是,如果你正在使用Comparable接口,你可以切换你在compareTo(Object bcomparable)中指定的输出值。

例如:

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= 1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= -1;
    return ret;
}

在我的程序中,量级是一个具有双数据类型的属性。这是对我定义的类频率按大小的倒序排序。因此,为了纠正这一点,您可以切换<和>返回的值。这将为您提供以下内容:

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= -1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= 1;
    return ret;
}

为了使用这个compareTo,我们简单地调用Arrays.sort(mFreq),它会给你排序的数组freq [] mFreq。

这个解决方案的美妙之处在于(在我看来),它可以用来对用户定义的类进行排序,甚至可以根据特定的属性对它们进行排序。如果可比接口的实现听起来让你望而生畏,我建议你不要这么想,事实并非如此。这个关于如何实现可比性的链接让我的事情变得简单多了。希望人们可以利用这个解决方案,你的快乐甚至可以和我的一样。

首先,你需要使用以下命令对数组进行排序:

Collections.sort(myArray);

然后你需要使用以下命令将升序颠倒为降序:

Collections.reverse(myArray);

我知道这里有很多答案,但仍然认为,没有人尝试使用核心java。 使用集合api,你最终会浪费这么多的内存和残差。

这是一个纯核心概念的尝试,是的,如果你更关心内存占用,这可能是更好的方法。

    int[] elements = new int [] {10,999,999,-58,548,145,255,889,1,1,4,5555,0,-1,-52};
    //int[] elements = null;
    
    if(elements != null && elements.length >1)
    {
        int max = 0, index = 0;
        for(int i =0;i<elements.length;i++)//find out what is Max
        {
            if(elements[i] > max)
                {
                    max = elements[i];
                    index = i;
                }
        }
        elements[index] = elements[0];//Swap the places
        elements[0] = max;
        for(int i =0;i < elements.length;i++)//loop over element
        {
            for(int j = i+1;j < elements.length;j++)//loop to compare the elements
            {
                if(elements[j] > elements[i])
                {
                    max = elements[j];
                    elements[j] = elements[i];
                    elements[i] = max;
                }
            }
        }
        
    }//i ended up using three loops and 2 extra variables
    System.out.println(Arrays.toString(elements));//if null it will print null
    // still love to learn more, please advise if we can do it better.

我也喜欢向你学习!

有一种方法可能会长一点,但它很有效。这是一个对int数组进行降序排序的方法。

希望有一天这能帮助到别人:

public static int[] sortArray (int[] array) {
    int [] sortedArray = new int[array.length];
    for (int i = 0; i < sortedArray.length; i++) {
        sortedArray[i] = array[i];
    }
    
    boolean flag = true;
    int temp;
    while (flag) {
        flag = false;
        for (int i = 0; i < sortedArray.length - 1; i++) {
            if(sortedArray[i] < sortedArray[i+1]) {
                temp = sortedArray[i];
                sortedArray[i] = sortedArray[i+1];
                sortedArray[i+1] = temp;
                flag = true;
            }
        }
    }
    
    return sortedArray;
    
}