??????????????????У??????????? ArrayList ??????????? 5 ?????????????? 5 ??????????????????? getArrayList()??sortAscending()?? sortDescending()?????????????Щ????????? ArrayList ????
??????????£?
????-------------------------------------------------------
????T E S T S
????-------------------------------------------------------
????Running guru.springframework.blog.sortarraylist.ascendingdescending.SortArrayListAscendingDescendingTest
????Unsorted ArrayList: [France?? USA?? India?? Spain?? England]
????Sorted ArrayList in Ascending Order : [England?? France?? India?? Spain?? USA]
????Sorted ArrayList in Descending Order: [USA?? Spain?? India?? France?? England]
????Tests run: 1?? Failures: 0?? Errors: 0?? Skipped: 0?? Time elapsed: 0.001 sec - in guru.springframework.blog.sortarraylis
???????????????????? ArrayList ?????????????????????????? Collections.sort()???????????????????? ArrayList ???????????????????????????????Щ?????????? ArrayList ????????
????Collections.sort() ?????? ArrayList ?????????κ????? List ???????????????????????????ζ????Щ???????????? java.lang ???е? Comparable ???????? String ??????? Comparable ?????????????????????? ArrayList ??????Щ???????? Java ??????? Comparable ???????????????????? Integer??Short??Double??Float??Boolean??BigInteger??BigDecimal??File ?? Date ??????? Comparable ????
???????Comparable????ArrayList
????Comparable ????е?? compareTo()????????????????? Comparable ?????????????????????????????б?????? Comparable ???????????д compareTo()?????????????????????????????????????????????????????????????????????compareTo()????????Int????????????????????????壺
?????????????????????? comPareTO()??????
?????????????????????? comPareTO()?????С
???????????????????
??????????????????????JobCandidate ?????????? ArrayList ?в???????????????JobCandidate ??????????????????????????????????????????????????????????? ArrayList ?е? JobCandidate ??????????????????????????? JobCandidate ????? Comparable ??????д compareTo()??????
????The code of the JobCandidate class is this.
????JobCandidate?????????£?
????JobCandidate.java
package guru.springframework.blog.sortarraylist.comparable;
public class JobCandidate implements Comparable {
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;
}
@Override
public int compareTo(JobCandidate candidate) {
return (this.getAge() < candidate.getAge() ? -1 :
(this.getAge() == candidate.getAge() ? 0 : 1));
}
@Override
public String toString() {
return " Name: " + this.name + "?? Gender: " + this.gender + "?? age:" + this.age;
}
}
?????????? JobCandidate ????д?? compareTo()?????У???????????????????????????????????????(this.getAge() – candidate.getAge())????????????????????????? return ??俴????????????????????????????????????????????????????????????????£???????????????????????????????????????????????Щ??????????????????????????????????????????????????????????????????????к???????????????????д????????????з???????? JobCandidate ???? ArrayList ???????????
????JobCandidateSorter.java
package guru.springframework.blog.sortarraylist.comparable;
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);
return jobCandidate;
}
}
?????? JobCandidateSorter ???У?????????????? ArrayList ??????з????????????????? JobCandidateSorter ??????????д?? getSortedJobCandidateByAge()????????????????У???????? Collections.sort()????????????????? ArrayList ????????????????? ArrayList??