[Java learning notes] implements the comparator interface for reverse string sorting.

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

// Create a comparator that returns the outcome
// Of a reverse string comparison.
Class revstrcomp implements comparator <string> {
// Implement the compare () method so that it
// Reverses the order of the string comparison.
Public int compare (string stra, string strb) {// compare strb to stra, rather than stra to strb.
Return strb. compareto (stra );
}
}

// Sort an array of strings in reverse order.
Import java. util .*;
// Create a comparator that returns the outcome
// Of a reverse string comparison.
Class revstrcomp implements comparator <string> {
// Implement the compare () method so that it
// Reverses the order of the string comparison.
Public int compare (string stra, string strb ){
// Compare strb to stra, rather than stra to strb.
Return strb. compareto (stra );
}
}
// Demonstrate the reverse string comparator.
Class revstrsort {
Public static void main (string ARGs []) {
// Create a sample array of strings.
String STRs [] = {"dog", "horse", "ZEBRA", "cow", "cat "};
// Show the initial order.
System. Out. Print ("initial order :");
For (string S: STRs)
System. Out. Print (S + "");
System. Out. println ("/N ");
// Sort the array in reverse order.
// Begin by creating a reverse string comparator.
Revstrcomp RSC = new revstrcomp ();

// Now, sort the strings using the reverse comparator.
Arrays. Sort (STRs, RSC );
// Show the reverse sorted order.
System. Out. Print ("sorted in reverse order :");
For (string S: STRs)
System. Out. Print (S + "");
System. Out. println ("/N ");
// For comparison, sort the strings in natural order.
Arrays. Sort (STRs );
// Show the natural sorted order.
System. Out. Print ("sorted in natural order :");
For (string S: STRs)
System. Out. Print (S + "");
System. Out. println ("/N ");
}
}

Output:

Initial order: Dog horse zebra cow cat
Sorted in reverse order: Zebra horse dog cow cat
Sorted in natural order: cat cow dog horse zebra

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.