Advertisement
AlanPriyatna

reverse sort

Dec 7th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class reversesort
  3. {
  4.     public static String reverse(String words){
  5.         char[] temparray = words.toCharArray();
  6.         int left, right=0;
  7.         right = temparray.length-1;
  8.  
  9.         for (left=0; left < right ; left++ ,right--)
  10.         {
  11.             // Swap values of left and right
  12.             char temp = temparray[left];
  13.             temparray[left] = temparray[right];
  14.             temparray[right]=temp;
  15.         }
  16.        
  17.         String res = new String(temparray);
  18.  
  19.         return res;
  20.     }
  21.     public static void main(String[] args)
  22.     {
  23.         int n;
  24.         String temp;
  25.         Scanner s = new Scanner(System.in);
  26.         n = s.nextInt(); //read numver of string
  27.         String names[] = new String[n];
  28.         String rnames[] = new String[n];
  29.         Scanner s1 = new Scanner(System.in);
  30.        //read all the names and reverse it
  31.         for(int i = 0; i < n; i++)
  32.         {
  33.             String inName = s1.nextLine();
  34.             names[i] = inName;
  35.             String rString = reverse(inName);
  36.             rnames[i] = rString;
  37.         }
  38.         for (int i = 0; i < n; i++)
  39.         {
  40.             for (int j = i + 1; j < n; j++)
  41.             {
  42.                 if (rnames[i].compareTo(rnames[j])>0)
  43.                 {
  44.                     temp = names[i];
  45.                     names[i] = names[j];
  46.                     names[j] = temp;
  47.                 }
  48.             }
  49.         }
  50.         System.out.println();
  51.         for (int i = 0; i < n - 1; i++)
  52.         {
  53.             System.out.println(names[i]);
  54.         }
  55.         System.out.print(names[n - 1]);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement