package com. dodoke. word;
import java. util. ArrayList;
import java. util. Collections;
import java. util. List;
public class Word {
public static void main ( String[ ] args) {
List< String> list = new ArrayList < String> ( ) ;
list. add ( "orange" ) ;
list. add ( "tomato" ) ;
list. add ( "apple" ) ;
list. add ( "litchi" ) ;
list. add ( "banana" ) ;
System. out. println ( "排序前:" ) ;
for ( String s : list) {
System. out. println ( s + " " ) ;
}
Collections. sort ( list) ;
System. out. println ( "排序后:" ) ;
for ( String s : list) {
System. out. println ( s + " " ) ;
}
}
}
package com. dodoke. students;
public class Students {
private int stuld;
private int age;
private String name;
public int getStuld ( ) {
return stuld;
}
public void setStuld ( int stuld) {
this . stuld = stuld;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Students ( ) {
super ( ) ;
}
public Students ( int stuld, int age, String name) {
super ( ) ;
this . stuld = stuld;
this . age = age;
this . name = name;
}
@Override
public String toString ( ) {
String str = "[学号:" + this . getStuld ( ) + "," ;
str += "姓名:" + this . getAge ( ) + "," ;
str += "成绩:" + this . getName ( ) + "]" ;
return str;
}
}
package com. dodoke. students;
import java. util. ArrayList;
import java. util. Collections;
import java. util. List;
public class StudentsTest {
public static void main ( String[ ] args) {
Students s1 = new Students ( 40 , 20 , "peter" ) ;
Students s2 = new Students ( 28 , 5 , "angel" ) ;
Students s3 = new Students ( 35 , 18 , "tom" ) ;
List< Students> studentsList = new ArrayList < Students> ( ) ;
studentsList. add ( s1) ;
studentsList. add ( s2) ;
studentsList. add ( s3) ;
System. out. println ( "排序前:" ) ;
for ( Students students : studentsList) {
System. out. println ( students) ;
}
Collections. sort ( studentsList, new NameComparator ( ) ) ;
System. out. println ( "排序后:" ) ;
for ( Students students : studentsList) {
System. out. println ( students) ;
}
}
}
package com. dodoke. students;
import java. util. Comparator;
public class NameComparator implements Comparator < Students> {
@Override
public int compare ( Students o1, Students o2) {
String name1 = o1. getName ( ) ;
String name2 = o2. getName ( ) ;
int n = name1. compareToIgnoreCase ( name2) ;
return n;
}
}
package com. dodoke. employee;
public class Employee implements Comparable < Employee> {
private String num;
private String name;
private double salary;
public String getNum ( ) {
return num;
}
public void setNum ( String num) {
this . num = num;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public double getSalary ( ) {
return salary;
}
public void setSalary ( double salary) {
this . salary = salary;
}
public Employee ( ) {
super ( ) ;
}
public Employee ( String num, String name, double salary) {
super ( ) ;
this . num = num;
this . name = name;
this . salary = salary;
}
@Override
public String toString ( ) {
String str = "[编号:" + this . getNum ( ) + "," ;
str += "姓名:" + this . getName ( ) + "," ;
str += "工资:" + this . getSalary ( ) + "]" ;
return str;
}
@Override
public int compareTo ( Employee o) {
double salary1 = this . getSalary ( ) ;
double salary2 = o. getSalary ( ) ;
int n = new Double ( salary2- salary1) . intValue ( ) ;
return n;
}
}
package com. dodoke. employee;
import java. util. ArrayList;
import java. util. Collections;
import java. util. List;
public class EmployeeTest {
public static void main ( String[ ] args) {
Employee e1 = new Employee ( "emp001" , "张三" , 1800.0 ) ;
Employee e2 = new Employee ( "emp002" , "李四" , 2500.0 ) ;
Employee e3 = new Employee ( "emp003" , "王五" , 1600.0 ) ;
List< Employee> list = new ArrayList < Employee> ( ) ;
list. add ( e1) ;
list. add ( e2) ;
list. add ( e3) ;
System. out. println ( "排序前:" ) ;
for ( Employee employee : list) {
System. out. println ( employee) ;
}
Collections. sort ( list) ;
System. out. println ( "排序后:" ) ;
for ( Employee employee : list) {
System. out. println ( employee) ;
}
}
}