Pro 2 - Write a program to sort a stream of Strings.
Sort String Array By Without Sort Method
public class StringSortingTest
{
public static void main(String args[])
{
String StringArray[]={"java","fore","cast","for","you"};
int n=StringArray.length;
String temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(StringArray[i].compareTo(StringArray[j])>0)
{
temp=StringArray[i];
StringArray[i]=StringArray[j];
StringArray[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
System.out.println(StringArray[i]);
}
}
}
Output:
cast
for
fore
java
you
Sort String Array By Sort Method
cast
for
fore
java
you
Sort String Array By Sort Method
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
// args is the list of guests
Arrays.sort(args);
for(int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
I ran that code using "java Test Bobby Joe Angel" and here is the output:
$ java Test Bobby Joe Angel
Angel
Bobby
Joe
No comments:
Post a Comment