Advertisement
Evyatar12

sort string

Oct 4th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. static String[] sort(String[] arr) {
  2.     // with lambda
  3.     Arrays.sort(arr, (a, b) -> a.compareTo(b));
  4.    
  5.     // without lambda
  6.     Arrays.sort(arr, new Comparator<String>() {
  7.         public int compare(String a, String b) {
  8.             return a.compareTo(b);
  9.         }
  10.     });
  11.     return arr;
  12. }
  13.    
  14. public static void main(String[] args) {
  15.     // prints [apple, banana, blue, green, yellow]
  16.     System.out.println(Arrays.asList(sort(new String[] { "banana", "apple", "yellow", "blue", "green" })));
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement