Skip to Content

Java Sort char Array Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.    Java Sort char Array Example
  3.    This example shows how to sort a char array using sort method of Arrays class of
  4.    java.util package.
  5. */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class SortCharArrayExample {
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create char array
  14.     char[] c1 = new char[]{'d','a','f','k','e'};
  15.    
  16.     //print original char array
  17.     System.out.print("Original Array :\t ");
  18.     for(int index=0; index < c1.length ; index++)
  19.       System.out.print("  "  + c1[index]);
  20.    
  21.     /*
  22.      To sort java char array use Arrays.sort() method of java.util package.
  23.      Sort method sorts char array in ascending order and based on quicksort
  24.      algorithm.
  25.      There are two static sort methods available in java.util.Arrays class
  26.      to sort a char array.
  27.     */
  28.    
  29.     //To sort full array use sort(char[] c) method.
  30.     Arrays.sort(c1);
  31.    
  32.     //print sorted char array
  33.     System.out.print("\nSorted char array :\t ");
  34.     for(int index=0; index < c1.length ; index++)
  35.       System.out.print("  "  + c1[index]);
  36.      
  37.     /*
  38.       To sort partial char array use
  39.       sort(char[] c, int startIndex, int endIndex) method where startIndex is
  40.       inclusive and endIndex is exclusive
  41.     */
  42.    
  43.     char[] c2 = new char[]{'d','a','f','k','e'};
  44.     Arrays.sort(c2,1,4);
  45.    
  46.     //print sorted char array
  47.     System.out.print("\nPartially Sorted char array :\t ");
  48.     for(int index=0; index < c2.length ; index++)
  49.       System.out.print("  "  + c2[index]);
  50.  
  51.   }
  52. }
  53.  
  54. /*
  55. Output Would be
  56.  
  57. Original Array :     d  a  f  k  e
  58. Sorted char array :     a  d  e  f  k
  59. Partially Sorted char array :     d  a  f  k  e
  60. */