[java]代码库
package s0403lambda表达式排序集合;
import java.util.Arrays;
public class Main {
public static void main(String[] args){
String[] players = {"Rafael Nadal", "Novak Djokovic",
"Stanislas Wawrinka", "David Ferrer",
"Roger Federer", "Andy Murray",
"Tomas Berdych", "Juan Martin Del Potro",
"Richard Gasquet", "John Isner"};
//运用了lambda表达式,比较简单的表达方式
// Comparator<String> sortByName = (String s1, String s2) -> (s1.compareTo(s2));
// Arrays.sort(players, sortByName);
Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2))); //最简单的表达方式
Arrays.asList(players).forEach( element->System.out.println(element) );//输出每一个元素
System.out.println();
System.out.println();
System.out.println();
// 3使用 lambda expression 排序,根据最后一个字母
Arrays.sort(players, (String s1, String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1)));
Arrays.asList(players).forEach( element->System.out.println(element) );//输出每一个元素
}
}