Friday 12 September 2014

Name Sorting in String

Structure of the Problem Requirements 

In this problem we will learn how to sort a string alphabetically. Just like Array sorting we also sort it with Bubble sort simple logic. A temporary string is used to rotate the alphabet and save them in array. Here is the source code of this problem which help you in better understanding.

Source Code



class SortString
{
               public static void main(String args[])
                  {
                     Stringstrings[]={"Naeem","Asad","Usman","Facebook","Twitter"};
                     String sort;
                     for(int x=0;x<strings.length;x++)
                       {
                          for(int y=x+1;y<strings.length;y++)
                               {
                            if(strings[x].compareTo(strings[y])>0)
                                     {
                                         sort=strings[x];
                                         strings[x]=strings[y];
                                         strings[y]=sort;
                                      }
                               }
                         }
                          System.out.println("Sorted Array of Strings:");
                          for(int z=0;z<strings.length;z++)
                                    System.out.println(strings[z]);
                    }
}

Output of the Program




Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment