I want to compare the two above string by their alphabetic order (which in this case "Project" then "Sunject" as "P" comes before "S").
Does anyone know how to do that in Java?
String a = "...";
String b = "...";
int compare = a.compareTo(b);
if (compare < 0) {
//a is smaller
}
else if (compare > 0) {
//a is larger
}
else {
//a is equal to b
}
//Get the Collator for US English and set its strength to PRIMARY
Collator usCollator = Collator.getInstance(Locale.US);
usCollator.setStrength(Collator.PRIMARY);
if( usCollator.compare("abc", "ABC") == 0 ) {
System.out.println("Strings are equivalent");
}
String s1 = "Project";
String s2 = "Sunject";
//print smaller one using compareTo() function
if(s1.compareTo(s2)<0) System.out.println(s1);
//if s1 is smaller then function returns negative which is less than 0 so s1
//is smaller
else System.out.println(s2); // else s2 is smaller
//print larger one using compareTo() function
if(s1.compareTo(s2)>0) System.out.println(s1);
//is s1 is larger function will give positive so print s1 else s2
else System.out.println(s2);
import java.util.Objects;
public class Person implements Comparable {
String firstName;
String lastName;
Integer age;
Integer DOBYear;
public Person(String firstName, String lastName, Integer age, Integer DOBYear) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.DOBYear = DOBYear;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getDOBYear() {
return DOBYear;
}
public void setDOBYear(Integer DOBYear) {
this.DOBYear = DOBYear;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName) && Objects.equals(age, person.age) && Objects.equals(DOBYear, person.DOBYear);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, age, DOBYear);
}
@Override
public int compareTo(Object o) {
Person p = (Person) o;
return p.getAge() > this.getAge() ? -1: p.getAge() == this.getAge() ? 0 : 1;
}
@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", DOBYear=" + DOBYear +
'}';
}
}
人名比较器
import java.util.Comparator;
public class PersonNameComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.getFirstName().compareTo(o2.getFirstName());
}
}
Java #
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class PersonTest {
public static void main(String args[]){
System.out.println("Hello World");
Person p1 = new Person("Umesh","Bhutada",31,1991);
Person p2 = new Person("Deepali","Baheti",29,1992);
Person p3 = new Person("Romeet","Zanwar",5,2017);
ArrayList<Person> arr1 = new ArrayList<Person>( Arrays.asList(p1,p2,p3));
Collections.sort(arr1);
arr1.forEach(person -> {System.out.println( person);});
System.out.println("End of World");
System.out.println("test 2");
Collections.sort(arr1,new PersonNameComparator());
arr1.forEach(person -> {System.out.println( person);});
}