-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathListSorting1.java
More file actions
69 lines (62 loc) · 1.65 KB
/
ListSorting1.java
File metadata and controls
69 lines (62 loc) · 1.65 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Program for sorting a given list of names in ascending order
import java.util.Scanner;
public class ListSorting1
{
public static void main(String args[])
{
System.out.print("Code for sorting the input name in ascending order ---");
Scanner reader = new Scanner(System.in);
//System.out.print("\nEnter the numbers of names: ");
//int num = reader.nextInt();
NameList n = new NameList();
n.input_names();
n.sort_names();
n.show_names();
}
}
class NameList
{
int num = 5;
String[] names = new String[num];
//NameList(int num)
//{
// num = num;
//}
void input_names()
{
Scanner reader = new Scanner(System.in);
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());
}
}
void sort_names()
{
//input_names();
// 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
//show_names();
}
void show_names()
{
System.out.print("\nSorted names are ---");
for (int i=0; i<num; i++)
{
System.out.print("\n" + names[i]);
}
}
}