-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListSorting.java
More file actions
50 lines (46 loc) · 1.37 KB
/
ListSorting.java
File metadata and controls
50 lines (46 loc) · 1.37 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
39
40
41
42
43
44
45
46
47
48
49
50
//Program for sorting a given list of names in ascending order
import java.util.Scanner;
public class ListSorting
{
public static void main(String args[])
{
System.out.print("Code for sorting the input name in ascending order ---");
NameList nl = new NameList();
nl.sort_names();
}
}
class NameList
{
void sort_names()
{
Scanner reader = new Scanner(System.in);
System.out.print("\nEnter the numbers of names: ");
int num = reader.nextInt();
String[] names = new String[num];
System.out.print("\nEnter the names -");
for (int i = 0; i < num; i++ )
{
System.out.print("\nEnter the name "+(i+1) + ": ");
names[i] = new String(reader.next());
}
// Code for sorting names
for (int i = 0; i<num; i++)
{
for (int j=i+1; j<num; j++)
{
if (names[i].compareTo(names[j]) > 0)
{
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
// End of Sorting codes
System.out.print("\nSorted names are -");
for (int i=0; i<num; i++)
{
System.out.print("\n" + names[i]);
}
}
}