Sunday 15 February 2015

Filled Under:

How To Sort Words Alphabetically In Java

This tutorial illustrate when to use compareToIgnoreCase in java in our last tutorial  we illustrate  How to sort string using compareTo  but in that tutorial words come first which are in upper case to ignore case transformation use this method  in this method words sort according to alphabetic order and ignore case.


CODING:

class ignore
{
public static void main(String args[])
{
String arr[] = {"e","d","f","b","a","F"};
System.out.println("Before sorting:");

for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j].compareToIgnoreCase(arr[i])<0) 
{
String t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}

OUTPUT:


0 comments:

Post a Comment