Skip to content

Instantly share code, notes, and snippets.

@Visgean
Created May 10, 2017 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Visgean/0b94c99a3aac8cf5e7ee38202d49725b to your computer and use it in GitHub Desktop.
Save Visgean/0b94c99a3aac8cf5e7ee38202d49725b to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CustomComparator {
public static void main(String[] args) {
String[] names = {"bba", "b", "aaaa", "ccc"};
List<String> name_list = Arrays.asList(names);
System.out.println("Before : "+Arrays.toString(name_list.toArray()));
Collections.sort(
name_list, (a,b) ->
a.length() < b.length() ? -1 : a.length() == b.length() ? 0 : 1
);
//return 1 if a should be before b
//return -1 if a should be before b
//return 0 otherwise
System.out.println("After : "+Arrays.toString(name_list.toArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment