Example usage for java.lang String compareTo

List of usage examples for java.lang String compareTo

Introduction

In this page you can find the example usage for java.lang String compareTo.

Prototype

public int compareTo(String anotherString) 

Source Link

Document

Compares two strings lexicographically.

Usage

From source file:Main.java

public static void main(String... args) {
    List<String> names = Arrays.asList("XML", "Java", "HTML", "CSS");
    Collections.sort(names, (String a, String b) -> b.compareTo(a));
    System.out.println(names);//w w w  .ja  v a 2  s.  c o m

    Collections.sort(names, (a, b) -> b.compareTo(a));
    System.out.println(names);

}

From source file:Main.java

public static void main(String args[]) {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy");
    java.util.Date date = new Date();
    df.applyPattern("EEE");
    String day = df.format(date);
    if (day.compareTo("Sat") == 0 || day.compareTo("Sun") == 0) {
        System.out.println(day + ": Weekend");
    } else {/*  w  ww  .  j  a  va 2 s  .  co m*/
        System.out.println(day + ": Weekday");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String s1 = "s1";
    String s2 = "s2";

    int i = s1.compareTo(s2); // 32; lowercase follows uppercase
    if (i < 0) {
        // s1 precedes s2
    } else if (i > 0) {
        // s1 follows s2
    } else {// ww  w . j a v a2 s .  c  o  m
        // s1 equals s2
    }

}

From source file:Main.java

public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = "java2s.com";
    System.out.println(str.compareTo(str2));

}

From source file:MainClass.java

public static void main(String[] arg) {

    String string1 = "abcde";
    String string2 = "bcdef";

    if (string1.compareTo(string2) > 0) {
        System.out.println("greater");
    }//  w  w  w .j  av  a  2 s.  c  om

    if (string1.compareTo(string2) == 0) {
        System.out.println("equal");
    }

    if (string1.compareTo(string2) < 0) {
        System.out.println("less");
    }

}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();

    map.put("KeyA", "erty");
    map.put("KeyC", "trwe");
    map.put("KeyD", "wert");
    map.put("KeyB", "fdsd");
    map.put("KeyS", "dsa");
    map.put("KeyE", "fdas");
    map.put("KeyG", "asdf");

    System.out.println(sort(map, new Comparator<String>() {
        @Override/*w w w . jav  a  2  s .c o m*/
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }));
}

From source file:Main.java

public static void main(String args[]) {
    JTextComponent component = new JTextArea();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }// w ww  .  j ava 2s  . c o  m
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:Main.java

public static void main(String args[]) {
    JTextComponent component = new JTextField();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }/*from ww w. ja v  a2s .co m*/
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:ListActionsJTextPane.java

public static void main(String args[]) {
    JTextComponent component = new JTextPane();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }//from   ww w.  j  av a 2  s  . c  o  m
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}

From source file:ListActionsJPasswordField.java

public static void main(String args[]) {
    JTextComponent component = new JPasswordField();

    // Process action list
    Action actions[] = component.getActions();
    // Define comparator to sort actions
    Comparator<Action> comparator = new Comparator<Action>() {
        public int compare(Action a1, Action a2) {
            String firstName = (String) a1.getValue(Action.NAME);
            String secondName = (String) a2.getValue(Action.NAME);
            return firstName.compareTo(secondName);
        }//  w  w w.ja va 2s  . c  o m
    };
    Arrays.sort(actions, comparator);

    int count = actions.length;
    System.out.println("Count: " + count);
    for (int i = 0; i < count; i++) {
        System.out.printf("%28s : %s\n", actions[i].getValue(Action.NAME), actions[i].getClass().getName());
    }
}