listDemo
package test1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
new ListExample().testList();
}
private void testList() {
List<Mother> list = new ArrayList<>();
list.add(new Mother("1"));
list.add(new Mother("3"));
list.add(new Mother("2"));
sortList(list);
printLIst(list);
}
private void printLIst(List<Mother> list) {
for (Mother obj : list) {
System.out.println(obj);
// obj.speak();
}
}
private void sortList(List<Mother> list) {
Collections.sort(list);
}
public class Mother implements Comparable<Mother> {
public Mother(String name2) {
this.name = name2;
}
public String name;
String foot;
String mouth = " ";
void speak() {
System.out.println(name + " is speaking.... hahahahaha....");
}
@Override
public String toString() {
return name + mouth;
}
@Override
public int compareTo(Mother o) {
return -this.name.compareTo(o.name);
}
}
}