Sorting A String Array In Java

No comments
In this particular article we will tell you, how to sort a string array in java,though it is not a very difficult task and has noting to do big in it,but most of the beginners are not aware about it.To sort a string array in java we can simply use sort method of Arrays class, and we will write a simple main class which has the whole code.
I am writing this article because sometimes it is really hard for some beginners to be aware about the complete java API and they get confused while doing such things and try to write their own code which is really painful.
Program-
package in.blogspot.tutorialsinn;

import java.util.Arrays;

public class SortingStringArray {

 public static void main(String[] args) {

  // creating an array of string
  String[] arr = { "java", "apple", "orange", "banana", "monkey" };

  // using sort method for sorting arr array
  Arrays.sort(arr);

  // printing the values of sorted array using loop
  for (String sort : arr) {
   System.out.println("" + sort);

  }
              }
        }


Though it was the simplest example of sorting , which has nothing to do more , but there are many problems on sorting which are not really easy like sorting an number array using loop,finding the longest string in an array and many more.But don't worry you will find the answers of all these problems in our upcoming articles.

No comments :

Post a Comment