-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingNames.java
More file actions
36 lines (34 loc) · 1.12 KB
/
SortingNames.java
File metadata and controls
36 lines (34 loc) · 1.12 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
package chapter02;
import java.util.Scanner;
/**
*
* @author harsh
*/
public class SortingNames {
private static void Sort(String[] ar,int size){
String temp;
for(int i=0;i<size;i++){
for(int j=i+1; j<size; j++){
if(ar[i].toLowerCase().compareTo(ar[j])>0){ //compares the string lexographically visit oracle.docs to find more of compareTo function
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter size : ");
int n = scan.nextInt();
String[] a = new String[n];
System.out.println("Enter elements");
String i=scan.nextLine(); //just a waste statement i just have to do it its ur choice
for(int k=0;k<n;k++)
a[k] = scan.nextLine();
Sort(a,n);
System.out.println("Sorted array : ");
for(int l=0;l<n;l++)
System.out.println(a[l]);
}
}