Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2008, 01:02 PM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
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:

Java Code
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:

Java Code
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:

Java Code
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



__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
Reply

Tags
java.util.arrays, sort arrays, sorting arrays

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert sort algorithm Alysosh Algorithms & Recursion 1 26-05-2009 02:28 PM
java.util.UnknownFormatConversionException error rosh72851 Exceptions 4 29-04-2009 03:53 PM
How to Populate all array values using the java.util.Arrays class JavaPF Java Code Snippets and Tutorials 0 07-04-2009 10:31 AM
New to Java/struggling with arrays Fendaril Collections and Generics 18 13-11-2008 12:31 PM
java.util.UnknownFormatConversionException error rosh72851 Exceptions 2 08-10-2008 04:01 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:53 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.