08 September 2018

Java Program to sort a list of names in Descending order

//Program to sort a list of names in Descending order
import java.util.*;
class Descendnames
     {
public static void main(String args[])
      {
    int n,i,j;
    System.out.println(" How many names you want to sort in Descending order:");
    Scanner obj=new Scanner(System.in);
    n=obj.nextInt();
    String temp,a[]=new String[n];//Declaring array of Strings
    Scanner obj1=new Scanner(System.in);
    System.out.println(" Enter names");
    for(i=0;i<n;i++)
        {
        a[i]=obj1.nextLine();
        }
    System.out.println(" Unsorted names : ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
    for (i = 0; i < n; i++)
            {
            for (j = i + 1; j < n; j++)
             {
                if (a[i].compareTo(a[j])<0) //compareTo() method is used for  comparing two //strings lexicographically.
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    System.out.println("\n Sorted names in Descending order ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
     System.out.println( );
      }
    }
The output will be similar to this


No comments:

Post a Comment

To convert from Upper case to lowercase(A to a)

//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...