Sort a String Array in JAVA.(Create a string array and sort all the string contained by the array in JAVA)
1 LOGIC: Using Sort Method of Arrays Class
CODING:
CODING:
CODING:
import java.util.Arrays;
class SortString
{
public static void main(String [] args)
{
String[] a={"Google","Facebook","Apple","Microsoft","Youtube","Twitter","Snapchat","Instagram","Whatsapp"};
System.out.println("Before Sorting Array:" + Arrays.toString(a));
Arrays.sort(a);
System.out.printf("After Sorting Array :" + Arrays.toString(a));
}
}
2 LOGIC: Without Using Sort Method of Arrays Classclass SortString
{
public static void main(String [] args)
{
String[] a={"Google","Facebook","Apple","Microsoft","Youtube","Twitter","Snapchat","Instagram","Whatsapp"};
System.out.println("Before Sorting Array:" + Arrays.toString(a));
Arrays.sort(a);
System.out.printf("After Sorting Array :" + Arrays.toString(a));
}
}
CODING:
class SortString
{
public static void main(String [] args)
{
String[] a={"Google","Facebook","Apple","Microsoft","Youtube","Twitter","Snapchat","Instagram","Whatsapp"};
String temp;
int i,r;
System.out.println("->Before Sorting List...");
for(i=0;i<10;i++)
System.out.println(a[i]);
for(i=0;i<10;i++)
{
for(r=i+1;r<10;r++)
{
if((int)a[i].charAt(0) > (int)a[r].charAt(0))
{
temp=a[i];
a[i]=a[r];
a[r]=temp;
}
}
}
System.out.println("->After Sorting List...");
for(i=0;i<10;i++)
System.out.println(a[i]);
}
}
{
public static void main(String [] args)
{
String[] a={"Google","Facebook","Apple","Microsoft","Youtube","Twitter","Snapchat","Instagram","Whatsapp"};
String temp;
int i,r;
System.out.println("->Before Sorting List...");
for(i=0;i<10;i++)
System.out.println(a[i]);
for(i=0;i<10;i++)
{
for(r=i+1;r<10;r++)
{
if((int)a[i].charAt(0) > (int)a[r].charAt(0))
{
temp=a[i];
a[i]=a[r];
a[r]=temp;
}
}
}
System.out.println("->After Sorting List...");
for(i=0;i<10;i++)
System.out.println(a[i]);
}
}
Comments
Post a Comment