サイトメニュー
|
|
public class Person implements Comparable {
private String id; // ID
public Person(String id, String name, String nameKana) {
this.id = id;
this.name = name;
this.nameKana = nameKana;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getNameKana() {
return nameKana;
}
public void setId(String string) {
id = string;
}
public void setName(String string) {
name = string;
}
public void setNameKana(String string) {
nameKana = string;
}
public String toString() {
}
public int compareTo(Object obj) {
return this.id.compareTo(((Person) obj).getId());
}
}
Person[] array = new Person[5];
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
List list = new ArrayList();
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
Person[] array = new Person[5];
Comparator asc = new Comparator() {
public int compare(Object obj0, Object obj1) {
String nameKana0 = ((Person) obj0).getNameKana();
String nameKana1 = ((Person) obj1).getNameKana();
int ret = 0;
if ((ret = nameKana0.compareTo(nameKana1)) == 0) {
String id0 = ((Person) obj0).getId();
String id1 = ((Person) obj1).getId();
ret = id0.compareTo(id1);
}
return ret;
}
};
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
Comparator desc = new Comparator() {
public int compare(Object obj0, Object obj1) {
String nameKana0 = ((Person) obj0).getNameKana();
String nameKana1 = ((Person) obj1).getNameKana();
int ret = 0;
if ((ret = nameKana1.compareTo(nameKana0)) == 0) {
String id0 = ((Person) obj0).getId();
String id1 = ((Person) obj1).getId();
ret = id1.compareTo(id0);
}
return ret;
}
};
for (int i = 0; i < array.length; i++)
System.out.println(array[i]); |