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

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


当前回答

这是我如何排序一个基本类型int数组。

int[] intArr = new int[] {9,4,1,7};
Arrays.sort(nums);
Collections.reverse(Arrays.asList(nums));

结果:

[1, 4, 7, 9]

其他回答

对于包含原语元素的数组,如果有org.apache.commons.lang(3)可供处置,则简单的反向数组(排序后)的方法是使用:

ArrayUtils.reverse(array);

另一个解决方案是,如果你正在使用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。

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

我有下面的工作解决方案

    public static int[] sortArrayDesc(int[] intArray){
    Arrays.sort(intArray);                      //sort intArray in Asc order
    int[] sortedArray = new int[intArray.length];   //this array will hold the sorted values

    int indexSortedArray = 0;
    for(int i=intArray.length-1 ; i >= 0 ; i--){    //insert to sortedArray in reverse order
        sortedArray[indexSortedArray ++] = intArray [i];
    }
    return sortedArray;
}

我知道这里有很多答案,但仍然认为,没有人尝试使用核心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.

我也喜欢向你学习!

这招对我很管用:

package doublearraysort;

import java.util.Arrays;
import java.util.Collections;

public class Gpa {


    public static void main(String[] args) {
        // initializing unsorted double array
        Double[] dArr = new Double[] {                 
            new Double(3.2),
            new Double(1.2),
            new Double(4.7),
            new Double(3.3),
            new Double(4.6),
           };
        // print all the elements available in list
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }

        // sorting the array
        Arrays.sort(dArr, Collections.reverseOrder());

        // print all the elements available in list again
        System.out.println("The sorted GPA Scores are:");
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }
    }
}

输出:

GPA = 3.2
GPA = 1.2
GPA = 4.7
GPA = 3.3
GPA = 4.6
The sorted GPA Scores are:
GPA = 4.7
GPA = 4.6
GPA = 3.3
GPA = 3.2
GPA = 1.2