-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAscendingorder.java
More file actions
38 lines (35 loc) · 1.13 KB
/
Ascendingorder.java
File metadata and controls
38 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Program for sorting a given list of names in ascending order
import java.util.Scanner;
public class Ascendingorder
{
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
System.out.print("\nEnter the numbers of names : ");
int n = reader.nextInt();
String[] names = new String[n];
System.out.print("\nEnter the names - ");
for (int i = 0; i < n; i++ )
{
System.out.print("\nEnter the name "+(i+1) + " : ");
names[i] = new String(reader.next());
}
for (int i = 0; i<n; i++)
{
for (int j=i+1; j<n; j++)
{
if (names[i].compareTo(names[j]) > 0)
{
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("\nSorted names are ---");
for (int i=0; i<n; i++)
{
System.out.print("\n" + names[i]);
}
}
}