Name Sorting in String

This is simple java exercise for beginners about string. The program sort the names in a given string.

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 on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment