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: How to Sort an Array using the java.util.Arrays class

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to Sort an Array using the java.util.Arrays class

    Here is a tutorial on how to use the java.util.Arrays class to sort Array values.

    This example sorts an Array of Integers from the smallest to largest value:

    import java.util.Arrays;
     
    public class HowToSortAnArray {
     
     public static void main(String[] args) {
     
      //Array of Integers
      int[] myIntArray = new int[]{20,100,69,4};
     
      //SORTS ARRAY FROM SMALLEST TO LARGEST INT
      Arrays.sort(myIntArray);
     
      //for loop to print Array values to console
      for (int a = 0; a < myIntArray.length; a++) {
       System.out.println(myIntArray[a]);
      }
     
     }
    }
    //Output: 4 20 69 100

    Using the same example as above but with a String Array, this will sort the values by case and then into alphabetical order:

    import java.util.Arrays;
     
    public class HowToSortAnArray {
     
     public static void main(String[] args) {
     
      String[] myStrArray = new String[] {"c", "b", "a", "D"};
     
      //SORTS STRING ARRAY INTO CASE & THEN ALPHABETICAL ORDER
      Arrays.sort(myStrArray);
     
      //for loop to print int values to console
      for (int a = 0; a < myStrArray.length; a++) {
       System.out.println(myStrArray[a]);
      }
     
     }
    }
    //Output: D a b c

    To sort into alphabetical order and be case insensitive you can use:

    import java.util.Arrays;
     
    public class HowToSortAnArray {
     
     public static void main(String[] args) {
     
      String[] myStrArray = new String[] {"c", "b", "a", "D"};
     
      //SORTS ARRAY INTO ALPHABETICAL ORDER
      Arrays.sort(myStrArray, String.CASE_INSENSITIVE_ORDER);
     
      //for loop to print int values to console
      for (int a = 0; a < myStrArray.length; a++) {
       System.out.println(myStrArray[a]);
      }
     
     }
    }
    //Output: a b c D
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Sort an Array using the java.util.Arrays class

    Thanks for the useful post.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:07 PM.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    jogja, Indonesia
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Sort an Array using the java.util.Arrays class

    thank you guss . . .

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. Replies: 3
    Last Post: April 26th, 2011, 02:51 AM
  3. Insert sort algorithm
    By Alysosh in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 26th, 2009, 09:28 AM
  4. Replies: 4
    Last Post: April 29th, 2009, 10:53 AM
  5. Details about 'CopyTo' of Arrays in Java
    By Fendaril in forum Collections and Generics
    Replies: 18
    Last Post: November 13th, 2008, 08:31 AM

Tags for this Thread