Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Sort Array in ascending and descding

Sort Array in ascending and descding

Scheduled Pinned Locked Moved Java
javadata-structureshelptutorial
11 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nt_virus
    wrote on last edited by
    #1

    Hi. I m running this code.

    import java.util.*;
    public class Main{
    public static void main(String[] args) {
    int[] value = {2,1,3,6,5};
    System.out.println(value); // Print unsorted array.
    Arrays.sort(value); // Sort array
    System.out.println(value); //
    }}

    The output is showing.. run: [I@3e25a5 [I@3e25a5 why is it so.. I m confused.. Please help me. Also please tell me how to do in descending order by user input. Collections class will do with List

    L 1 Reply Last reply
    0
    • N nt_virus

      Hi. I m running this code.

      import java.util.*;
      public class Main{
      public static void main(String[] args) {
      int[] value = {2,1,3,6,5};
      System.out.println(value); // Print unsorted array.
      Arrays.sort(value); // Sort array
      System.out.println(value); //
      }}

      The output is showing.. run: [I@3e25a5 [I@3e25a5 why is it so.. I m confused.. Please help me. Also please tell me how to do in descending order by user input. Collections class will do with List

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      nt_virus wrote:

      why is it so

      You cannot print an array or integers directly in this way. You need to iterate the array and print each individual value after you have converted it to a character string. Take a look at some of the tutorials on the web.

      N 1 Reply Last reply
      0
      • L Lost User

        nt_virus wrote:

        why is it so

        You cannot print an array or integers directly in this way. You need to iterate the array and print each individual value after you have converted it to a character string. Take a look at some of the tutorials on the web.

        N Offline
        N Offline
        nt_virus
        wrote on last edited by
        #3

        thanks I did ascending..

        public class Main{
        public static void main(String[] args) {
        int[] value = {2,1,3,6,5};
        System.out.println("Unsorted value"); // Print unsorted array.
        for (int s : value)
        System.out.print(s + " ");
        System.out.println("\n");
        Arrays.sort(value); // Sort array

            System.out.println("sorted value ascending");       // Print unsorted array.
            for (int s : value)
                System.out.print(s + " ");
            System.out.println("\\n");      
        }}
        

        please tell how to do descending...

        L 1 Reply Last reply
        0
        • N nt_virus

          thanks I did ascending..

          public class Main{
          public static void main(String[] args) {
          int[] value = {2,1,3,6,5};
          System.out.println("Unsorted value"); // Print unsorted array.
          for (int s : value)
          System.out.print(s + " ");
          System.out.println("\n");
          Arrays.sort(value); // Sort array

              System.out.println("sorted value ascending");       // Print unsorted array.
              for (int s : value)
                  System.out.print(s + " ");
              System.out.println("\\n");      
          }}
          

          please tell how to do descending...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          nt_virus wrote:

          please tell how to do descending...

          Well I guess you need to look at the documentation for the Arrays.sort() method.

          N 1 Reply Last reply
          0
          • L Lost User

            nt_virus wrote:

            please tell how to do descending...

            Well I guess you need to look at the documentation for the Arrays.sort() method.

            N Offline
            N Offline
            nt_virus
            wrote on last edited by
            #5

            I read the documentation now, I didn't find descending. There is a way by Comparator .. but its lengthy .. Any other way .. please tell.

            L 1 Reply Last reply
            0
            • N nt_virus

              I read the documentation now, I didn't find descending. There is a way by Comparator .. but its lengthy .. Any other way .. please tell.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              nt_virus wrote:

              There is a way by Comparator .. but its lengthy .. Any other way .. please tell.

              You really think this[^] is lengthy? Or is it the reading of the documentation that's lengthy?

              N 1 Reply Last reply
              0
              • L Lost User

                nt_virus wrote:

                There is a way by Comparator .. but its lengthy .. Any other way .. please tell.

                You really think this[^] is lengthy? Or is it the reading of the documentation that's lengthy?

                N Offline
                N Offline
                nt_virus
                wrote on last edited by
                #7

                I tried that thing earlier but as I declared array as.. int[] value = {2,1,3,6,5} So this line Arrays.sort(value, Collections.reverseOrder()); giving error, red line under value, I don't understand why.. But now I declare array as .. Integer[] value = {2,1,3,6,5} It works....

                /*
                * To change this template, choose Tools | Templates
                * and open the template in the editor.
                */

                package SortListOfNumbers;
                import java.util.Arrays;
                import java.util.Collections;

                public class Main{
                public static void main(String[] args)
                {
                Sorting srt = new Sorting();
                srt.ascending();
                srt.descending();
                }
                }
                class Sorting{
                public void ascending(){

                        int\[\] value = {2,1,3,6,5};
                        System.out.println("Sorting in ascending order");
                        System.out.println("Unsorted value");       // Print unsorted array.
                         for (int s : value)
                           System.out.print(s + " ");
                         System.out.println("\\n");
                         Arrays.sort(value); // Sort in ascending
                         System.out.println("sorted value ascending");       // Print unsorted array.
                         for (int s : value)
                                System.out.print(s + " ");
                         System.out.println("\\n");
                 }
                 public void descending(){
                
                        Integer\[\] value = {2,1,3,6,5};
                        System.out.println("Sorting in descending order");
                        System.out.println("Unsorted value");       // Print unsorted array.
                         for (int s : value)
                           System.out.print(s + " ");
                         System.out.println("\\n");
                         Arrays.sort(value, Collections.reverseOrder());
                         System.out.println("Sorted Descending");       // Print unsorted array.
                         for (int s : value)
                           System.out.print(s + " ");
                         System.out.println("\\n");
                 }}
                

                Please explain why is it happen so.. it bit strange for me. and thanks a lot.

                L 2 Replies Last reply
                0
                • N nt_virus

                  I tried that thing earlier but as I declared array as.. int[] value = {2,1,3,6,5} So this line Arrays.sort(value, Collections.reverseOrder()); giving error, red line under value, I don't understand why.. But now I declare array as .. Integer[] value = {2,1,3,6,5} It works....

                  /*
                  * To change this template, choose Tools | Templates
                  * and open the template in the editor.
                  */

                  package SortListOfNumbers;
                  import java.util.Arrays;
                  import java.util.Collections;

                  public class Main{
                  public static void main(String[] args)
                  {
                  Sorting srt = new Sorting();
                  srt.ascending();
                  srt.descending();
                  }
                  }
                  class Sorting{
                  public void ascending(){

                          int\[\] value = {2,1,3,6,5};
                          System.out.println("Sorting in ascending order");
                          System.out.println("Unsorted value");       // Print unsorted array.
                           for (int s : value)
                             System.out.print(s + " ");
                           System.out.println("\\n");
                           Arrays.sort(value); // Sort in ascending
                           System.out.println("sorted value ascending");       // Print unsorted array.
                           for (int s : value)
                                  System.out.print(s + " ");
                           System.out.println("\\n");
                   }
                   public void descending(){
                  
                          Integer\[\] value = {2,1,3,6,5};
                          System.out.println("Sorting in descending order");
                          System.out.println("Unsorted value");       // Print unsorted array.
                           for (int s : value)
                             System.out.print(s + " ");
                           System.out.println("\\n");
                           Arrays.sort(value, Collections.reverseOrder());
                           System.out.println("Sorted Descending");       // Print unsorted array.
                           for (int s : value)
                             System.out.print(s + " ");
                           System.out.println("\\n");
                   }}
                  

                  Please explain why is it happen so.. it bit strange for me. and thanks a lot.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  nt_virus wrote:

                  Please explain why is it happen so.. it bit strange for me.

                  Probably because int is a simple type, but Integer is a class. I would suggest reading the Java documentation for a full description of the differences and what methods and interfaces they support.

                  4 1 Reply Last reply
                  0
                  • L Lost User

                    nt_virus wrote:

                    Please explain why is it happen so.. it bit strange for me.

                    Probably because int is a simple type, but Integer is a class. I would suggest reading the Java documentation for a full description of the differences and what methods and interfaces they support.

                    4 Offline
                    4 Offline
                    4277480
                    wrote on last edited by
                    #9

                    I looked at your code, you need to read the error message which says The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Comparator < Object > ) so when you added the Integer you are solving the problem that was generated because Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

                    1 Reply Last reply
                    0
                    • N nt_virus

                      I tried that thing earlier but as I declared array as.. int[] value = {2,1,3,6,5} So this line Arrays.sort(value, Collections.reverseOrder()); giving error, red line under value, I don't understand why.. But now I declare array as .. Integer[] value = {2,1,3,6,5} It works....

                      /*
                      * To change this template, choose Tools | Templates
                      * and open the template in the editor.
                      */

                      package SortListOfNumbers;
                      import java.util.Arrays;
                      import java.util.Collections;

                      public class Main{
                      public static void main(String[] args)
                      {
                      Sorting srt = new Sorting();
                      srt.ascending();
                      srt.descending();
                      }
                      }
                      class Sorting{
                      public void ascending(){

                              int\[\] value = {2,1,3,6,5};
                              System.out.println("Sorting in ascending order");
                              System.out.println("Unsorted value");       // Print unsorted array.
                               for (int s : value)
                                 System.out.print(s + " ");
                               System.out.println("\\n");
                               Arrays.sort(value); // Sort in ascending
                               System.out.println("sorted value ascending");       // Print unsorted array.
                               for (int s : value)
                                      System.out.print(s + " ");
                               System.out.println("\\n");
                       }
                       public void descending(){
                      
                              Integer\[\] value = {2,1,3,6,5};
                              System.out.println("Sorting in descending order");
                              System.out.println("Unsorted value");       // Print unsorted array.
                               for (int s : value)
                                 System.out.print(s + " ");
                               System.out.println("\\n");
                               Arrays.sort(value, Collections.reverseOrder());
                               System.out.println("Sorted Descending");       // Print unsorted array.
                               for (int s : value)
                                 System.out.print(s + " ");
                               System.out.println("\\n");
                       }}
                      

                      Please explain why is it happen so.. it bit strange for me. and thanks a lot.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      See further information below from Member 4277480.

                      N 1 Reply Last reply
                      0
                      • L Lost User

                        See further information below from Member 4277480.

                        N Offline
                        N Offline
                        nt_virus
                        wrote on last edited by
                        #11

                        Thanks a lot a lot to both of you guys.. You made my day..

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups