Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Saturday, 30 December 2017

Java Program to Sort Names in Lexical Order

  1. class LexicalOrder
  2. {
  3. public static void main(String[] args) throws Exception
  4. {
  5. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6. System.out.print("\nEnter The number of Names :");
  7. int n = Integer.parseInt(br.readLine());
  8. String names[] = new String[n];
  9. for (int i = 1; i <= n; i++)
  10. {
  11. System.out.print("Enter Name " + i + ":");
  12. names[i-1] = br.readLine();
  13. }
  14. System.out.println("\nNames in Ascending Order");
  15. System.out.println();
  16. for (int j = 0; j < names.length; j++)
  17. {
  18. for (int i = j + 1; i < names.length; i++)
  19. {
  20. if (names[i].compareToIgnoreCase(names[j]) < 0)
  21. {
  22. String temp = names[j];
  23. names[j] = names[i];
  24. names[i] = temp;
  25. }
  26. }
  27. System.out.println(names[j]);
  28. }
  29. }
  30. }

No comments:

Post a Comment