Utils.java 916 B

1234567891011121314151617181920212223242526272829
  1. import java.util.*;
  2. public class Utils {
  3. static Comparator<String> compareByNumber() {
  4. return new Comparator<String>() {
  5. @Override
  6. public int compare(String o1, String o2) {
  7. return extractInt(o1) - extractInt(o2);
  8. }
  9. private int extractInt(String s) {
  10. String num = s.replaceAll("\\D", "");
  11. return num.isEmpty() ? 0 : Integer.parseInt(num);
  12. }
  13. };
  14. }
  15. static Comparator<Object> compareByUnitName() {
  16. return new Comparator<Object>() {
  17. private final List<String> ORDER = Arrays.asList("seconds", "minutes", "hours", "days", "weeks", "months", "years");
  18. @Override
  19. public int compare(Object o1, Object o2) {
  20. return Integer.compare(ORDER.indexOf(o1.toString()), ORDER.indexOf(o2.toString()));
  21. }
  22. };
  23. }
  24. }