A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Sort an Array of Strings in Reverse OrderLearn how to sort an array efficiently using the sort() method and a custom Comparator in Java. |
On Saturday, February 2nd 2008 at 12:51 AM By Louis Fernandez (View Profile) ![]() ![]() ![]() ![]() (Rated 4.7 with 6 votes) |
||
|
Sorting is a common task in programming, and sorting arrays of strings is no exception. For example, you might want to sort a list of the items sold by an online store or a list of customer names and e-mail addresses. Fortunately, Java makes sorting arrays of string
easy because it provides the sort( ) utility method, which is defined by the Arrays class in
java.util. In its default form, sort( ) orders strings in case-sensitive, alphabetical order, and
this is fine for many situations. However, sometimes you want to sort an array of strings in
reverse alphabetical order. This requires a bit more work.
There are a number of ways to approach the problem of sorting in reverse. For example,
one naive solution is to sort the array and then copy it back to front into another array.
Besides lacking elegance, this technique is also inefficient. Fortunately, Java provides a
simple, yet effective way to reverse-sort an array of strings. This approach uses a custom
Comparator to specify how the sort should be conducted and a version of sort( ) that takes
the Comparator as an argument. Step-by-Step Discussion Example The output from this program is shown here:
|
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsThank you for kind information
nice explanation of how Comparators work. Instead of implementing your own reverse Comparator, java provides one for the natural ordering using
Collections.reverseOrder()
or another one to reverse any comparator
Collections.reverseOrder(Comparator c)
the programme is not running!
good
sir how about 1 input of word and sort them in alpha or viceversa...
like when i input word KEFKA the out put should AEFKK or KKFEA
can you make me one...
how to creat a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of two numbers?
how to creat a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of two numbers?
Modify the program by reversing the third name entered into the array.
public class TMA_Q3{
public static void main(String[]args){
String Name1,Name2,Name3,Name4,Name5,outputStr;
Name1 = JOptionPane.showInputDialog(null,"Enter First Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name2 = JOptionPane.showInputDialog(null,"Enter Second Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name3 = JOptionPane.showInputDialog(null,"Enter Third Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name4 = JOptionPane.showInputDialog(null,"Enter Fourth Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name5 = JOptionPane.showInputDialog(null,"Enter Fifth Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
String[]Name = {Name1,Name2,Name3,Name4,Name5};
outputStr = " Secondly Entered Name is : " Name[1] "\n" ;
JOptionPane.showMessageDialog(null, outputStr ," Secondly Entered Name ", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Thirdly Entered Name is: " Name[2]);
String [] thirdname = {Name3};
thirdname[0]= thirdname[0].substring(0,8);
System.out.println("Length of Third Name: " thirdname.length);
System.out.println("\n " thirdname[0]);
for(int i=8 ; i>=0; i--)
{System.out.print(" " thirdname[i]);}
}
}
Modify the program by reversing the third name entered into the array.
public class TMA_Q3{
public static void main(String[]args){
String Name1,Name2,Name3,Name4,Name5,outputStr;
Name1 = JOptionPane.showInputDialog(null,"Enter First Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name2 = JOptionPane.showInputDialog(null,"Enter Second Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name3 = JOptionPane.showInputDialog(null,"Enter Third Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name4 = JOptionPane.showInputDialog(null,"Enter Fourth Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
Name5 = JOptionPane.showInputDialog(null,"Enter Fifth Name :","TMA3_Q3",JOptionPane.QUESTION_MESSAGE);
String[]Name = {Name1,Name2,Name3,Name4,Name5};
outputStr = " Secondly Entered Name is : " Name[1] "\n" ;
JOptionPane.showMessageDialog(null, outputStr ," Secondly Entered Name ", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Thirdly Entered Name is: " Name[2]);
String [] thirdname = {Name3};
thirdname[0]= thirdname[0].substring(0,8);
System.out.println("Length of Third Name: " thirdname.length);
System.out.println("\n " thirdname[0]);
for(int i=8 ; i>=0; i--)
{System.out.print(" " thirdname[i]);}
}
}
Related Tutorials
Related Source Code
Java Job Search