Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sorting arrays in descending order with Collections class in Java

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Sorting arrays in descending order with Collections class in Java

    Hello friends am trying to create a java program to sort an array in ascending and descending order
    here is the program I created


    import java.util.Arrays;
    import java.util.Collections;
    public class ArraySort
    {
    public static void main(String [] args)
    {
    int [] num = {5,9,1,65,7,8,9};
    Arrays.sort(num);
    System.out.println("The rsorted array in ascending order");
    for(int i = 0; i<num.length; i++)
    {
    System.out.println(num[i]);
    }
    System.out.println("The rsorted array in descending order");
    Arrays.sort(num,Collections.reverseOrder(num));
    for(int i = 0; i<num.length; i++)
    {
    System.out.println(num[i]);
    }
    }
    }

    BUT I GET THE FOLLOWING EROOR ON COMPILATION

    ArraySort.java:12: error: no suitable method found for reverseOrder(int[])
    Arrays.sort(num,Collections.reverseOrder(num));
    ^
    method Collections.<T#1>reverseOrder(Comparator<T#1>) is not applicable
    (no instance(s) of type variable(s) T#1 exist so that argument type int[] conforms to formal parameter type Comparator<T#1>)
    method Collections.<T#2>reverseOrder() is not applicable
    (cannot instantiate from arguments because actual and formal argument lists differ in length)
    where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>reverseOrder(Comparator<T#1>)
    T#2 extends Object declared in method <T#2>reverseOrder()
    1 error
    What's wrong with my program? Suggest a solution for me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting arrays in descending order with Collections class in Java

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    error: no suitable method found for reverseOrder(int[])
    Where is that method with that args defined? Read the API doc for the method to see what args it takes.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sorting arrays in descending order with Collections class in Java

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Similar Threads

  1. Replies: 1
    Last Post: April 6th, 2014, 07:33 AM
  2. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  3. Replies: 1
    Last Post: September 26th, 2012, 12:31 PM
  4. Problem with sorting by alphabetical order
    By pikapika in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 6th, 2012, 09:58 PM
  5. Replies: 3
    Last Post: October 22nd, 2011, 01:53 AM