【Java学习笔记】实现Comparator接口来进行字符串逆向排序
2012-12-17 21:21
711 查看
作者:gnuhpc
出处: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");
}
}
输出为:
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
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
出处: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");
}
}
输出为:
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
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
相关文章推荐
- Java技巧——实现Comparator接口来进行字符串逆向排序
- 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理
- Java编程之TreeSet排序两种解决方法(1)元素自身具备比较功能,元素需要实现Comparable接口覆盖compare(2)创建根据自定义Person类的name进行排序的Comparator
- java 学习笔记--Comparator中为什么其他类实现这个接口为什么没有复写equals(Object obj)这个方法?
- 【Java学习笔记】Comparable接口的实现和使用
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- 【Java学习笔记】Comparable接口的实现和使用
- java 实现Comparator接口排序
- 学习java笔记 --- 一个实现Iterable<E>接口的小范例
- 【Java学习笔记】32:List接口及其实现类
- Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- Java学习笔记_22_Set接口的实现类
- Java 学习笔记--解决一个类实现多个接口的问题
- 【Java学习笔记】使用Collator进行本地化语言的排序
- java实现对一个字符串中的数值进行从小到大的排序
- 8大内部排序算法学习笔记--(2)快速排序 Java实现
- Thinking in Java学习笔记 Thread.UncaughtExceptionHandler接口实现捕获线程内异常
- 学习笔记5:java 1.8 Predicate,Function,Supplier,Consumer,Comparator,Optional,Stream接口的使用
- Java实现对字符串中的数值进行排序操作示例