????????????????д??????????????????????????
????JobCandidateSorterTest.java
package guru.springframework.blog.sortarraylist.comparable;
import org.junit.Test;
import java.lang.reflect.Array;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class JobCandidateSorterTest {
<a href="http://www.jobbole.com/members/madao">@Test</a>
public void testGetSortedJobCandidateByAge() throws Exception {
JobCandidate jobCandidate1 = new JobCandidate("Mark Smith"?? "Male"?? 26);
JobCandidate jobCandidate2 = new JobCandidate("Sandy Hunt"?? "Female"?? 23);
JobCandidate jobCandidate3 = new JobCandidate("Betty Clark"?? "Female"?? 20);
JobCandidate jobCandidate4 = new JobCandidate("Andrew Styne"?? "Male"?? 24);
ArrayList jobCandidateList = new ArrayList&lt;&gt;();
jobCandidateList.add(jobCandidate1);
jobCandidateList.add(jobCandidate2);
jobCandidateList.add(jobCandidate3);
jobCandidateList.add(jobCandidate4);
JobCandidateSorter jobCandidateSorter = new JobCandidateSorter(jobCandidateList);
ArrayList sortedJobCandidate = jobCandidateSorter.getSortedJobCandidateByAge();
System.out.println("-----Sorted JobCandidate by age: Ascending-----");
for (JobCandidate jobCandidate : sortedJobCandidate) {
System.out.println(jobCandidate);
}
}
}
?????????????????У????????????? JobCandidate ?????????????? ArrayList?????????? ArrayList ??????????????? JobCandidateSorter ??????????? JobCandidateSorter ??? getSortedJobCandidateByAge()????????????????????????????? ArrayList????????????????£?
????-------------------------------------------------------
????T E S T S
????-------------------------------------------------------
????Running guru.springframework.blog.sortarraylist.comparable.JobCandidateSorterTest
????-----Sorted JobCandidate by age: Ascending-----
????Name: Betty Clark?? Gender: Female?? age:20
????Name: Sandy Hunt?? Gender: Female?? age:23
????Name: Andrew Styne?? Gender: Male?? age:24
????Name: Mark Smith?? Gender: Male?? age:26
????Tests run: 1?? Failures: 0?? Errors: 0?? Skipped: 0?? Time elapsed: 0.003 sec
????- in guru.springframework.blog.sortarraylist.comparable.JobCandidateSorterTest
??????? Comparable ?? ArrayList ?????????????????????????????????Щ??????????????????????????? Comparable ????д compareTo()???????????????ζ????????????????????????????(?????????е????????)?????????????????????????? JobCandidate ???????????????? Comparable ????????????????????????????????б?????????????????????????????????????????Java ???????? java.util ????????Comparator???????????????????
??????? Comparator ???? ArrayList
????Comparator ?????Comparable ????????????????????????????? compare()????????? Comparable?? compareTo()??????????????? compare()??????????????????????б???
??????????? Comparator ??????????ù?????? JobCandidate ??????????????????????? Comparatoras ?????????????? JobCandidate ???????????????????????
??????????????? Comparator ?? JobCandidate ?????
????JobCandidate.java
package guru.springframework.blog.sortarraylist.comparator;
import java.util.Comparator;
public class JobCandidate {
private String name;
private String gender;
private int age;
public JobCandidate(String name?? String gender?? int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public static Comparator ageComparator = new Comparator() {
@Override
public int compare(JobCandidate jc1?? JobCandidate jc2) {
return (jc2.getAge() < jc1.getAge() ? -1 :
(jc2.getAge() == jc1.getAge() ? 0 : 1));
}
};
public static Comparator nameComparator = new Comparator() {
@Override
public int compare(JobCandidate jc1?? JobCandidate jc2) {
return (int) (jc1.getName().compareTo(jc2.getName()));
}
};
@Override
public String toString() {
return " Name: " + this.name + "?? Gender: " + this.gender + "?? age:" + this.age;
}
}
??????????????У??? 29 ?е? 35 ?У?????д???????????????? compare()??????????????????? JobCandidate ?????????????37?е?42?У???????д???????????????? compare() ????????????????????? JobCandidate????????????????д????????з??? ArrayList ????????????
????JobCandidateSorter.java
package guru.springframework.blog.sortarraylist.comparator;
import java.util.ArrayList;
import java.util.Collections;
public class JobCandidateSorter {
ArrayList jobCandidate = new ArrayList<>();
public JobCandidateSorter(ArrayList jobCandidate) {
this.jobCandidate = jobCandidate;
}
public ArrayList getSortedJobCandidateByAge() {
Collections.sort(jobCandidate?? JobCandidate.ageComparator);
return jobCandidate;
}
public ArrayList getSortedJobCandidateByName() {
Collections.sort(jobCandidate?? JobCandidate.nameComparator);
return jobCandidate;
}
}
??????????????У?????д?? getSortedJobCandidateByAge()??????????????????????????? Collections.sort()??????汾??????汾???????????? ArrayList ???????????? Comparator ?????? getSortedJobCandidateByName()???????????????????? Collections.sort()???????????汾??????汾???????????? ArrayList ????????????? Comparator ????
????Let’s write a test class to test our code.
??????????д???????????????????????
????JobCandidateSorterTest.java
package guru.springframework.blog.sortarraylist.comparator;
import guru.springframework.blog.sortarraylist.comparator.JobCandidate;
import guru.springframework.blog.sortarraylist.comparator.JobCandidateSorter;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class JobCandidateSorterTest {
JobCandidateSorter jobCandidateSorter;
@Before
public void setUp() throws Exception {
JobCandidate jobCandidate1 = new JobCandidate("Mark Smith"?? "Male"?? 26);
JobCandidate jobCandidate2 = new JobCandidate("Sandy Hunt"?? "Female"?? 23);
JobCandidate jobCandidate3 = new JobCandidate("Betty Clark"?? "Female"?? 20);
JobCandidate jobCandidate4 = new JobCandidate("Andrew Styne"?? "Male"?? 24);
ArrayList jobCandidateList = new ArrayList&lt;&gt;();
jobCandidateList.add(jobCandidate1);
jobCandidateList.add(jobCandidate2);
jobCandidateList.add(jobCandidate3);
jobCandidateList.add(jobCandidate4);
jobCandidateSorter = new JobCandidateSorter(jobCandidateList);
}
<a href="http://www.jobbole.com/members/madao">@Test</a>
public void testGetSortedJobCandidateByAge() throws Exception {
System.out.println("-----Sorted JobCandidate by age: Descending-----");
ArrayList sortedJobCandidate = jobCandidateSorter.getSortedJobCandidateByAge();
for (JobCandidate jobCandidate : sortedJobCandidate) {
System.out.println(jobCandidate);
}
}
<a href="http://www.jobbole.com/members/madao">@Test</a>
public void testGetSortedJobCandidateByName() throws Exception {
System.out.println("-----Sorted JobCandidate by name: Ascending-----");
ArrayList sortedJobCandidate = jobCandidateSorter.getSortedJobCandidateByName();
for (JobCandidate jobCandidate : sortedJobCandidate) {
System.out.println(jobCandidate);
}
}
}
??????????????????? ArrayList ???????? JobCandidate ????????? Before ???????????? setup()?????д???????? JobCandidateSorter ?????????????? Junit ?????????ο???????????°??? Junit ????Junit ?????????У????? testGetSortedJobCandidateByAge()?????????????????? getSortedJobCandidateByAge()?????????????÷?????????????? ArrayList???? testGetSortedJobCandidateByName()??????????????????getSortedJobCandidateByName()?????????????÷???????? ArrayList?????????????£?
????-------------------------------------------------------
????T E S T S
????-------------------------------------------------------
????Running guru.springframework.blog.sortarraylist.comparator.JobCandidateSorterTest
????-----Sorted JobCandidate by name: Ascending-----
????Name: Andrew Styne?? Gender: Male?? age:24
????Name: Betty Clark?? Gender: Female?? age:20
????Name: Mark Smith?? Gender: Male?? age:26
????Name: Sandy Hunt?? Gender: Female?? age:23
????-----Sorted JobCandidate by age: Descending-----
????Name: Mark Smith?? Gender: Male?? age:26
????Name: Andrew Styne?? Gender: Male?? age:24
????Name: Sandy Hunt?? Gender: Female?? age:23
????Name: Betty Clark?? Gender: Female?? age:20
????Tests run: 2?? Failures: 0?? Errors: 0?? Skipped: 0?? Time elapsed: 0.023 sec
????- in guru.springframework.blog.sortarraylist.comparator.JobCandidateSorterTest
???????
???????????????????? ArrayList ????????????????????? Comparable ?????????? Comparator????????????????????????????????????????ü????????? Comparable ????????“???????????????????????”????? Comparator ????????“???????????????????”?????????????????????á?????????????????????????