オブジェクトをソートする方法
5548 ワード
オブジェクトをバブル法でソートします.
public class Job {
private String no;
private String name;
private String grade;
private String ranking;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getRanking() {
return ranking;
}
public void setRanking(String ranking) {
this.ranking = ranking;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((grade == null) ? 0 : grade.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((no == null) ? 0 : no.hashCode());
result = prime * result + ((ranking == null) ? 0 : ranking.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Job other = (Job) obj;
if (grade == null) {
if (other.grade != null)
return false;
} else if (!grade.equals(other.grade))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no == null) {
if (other.no != null)
return false;
} else if (!no.equals(other.no))
return false;
if (ranking == null) {
if (other.ranking != null)
return false;
} else if (!ranking.equals(other.ranking))
return false;
return true;
}
@Override
public String toString() {
return "Job [no=" + no + ", name=" + name + ", grade=" + grade
+ ", ranking=" + ranking + "]";
}
}
import java.util.List;
public class TestSort {
public static Job[] mySort(List<Job> list) {
int len = list.size();
Job[] array = new Job[len];
for (int i = 0; i < len; i++) {
array[i] = list.get(i);
}
Job temp = null;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - 1; j++) {
int grade1 = Integer.parseInt(array[i].getGrade());
int grade2 = Integer.parseInt(array[j].getGrade());
if (grade1 > grade2) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
public static Job[] paixu(Job[] array) {
int len = array.length;
for (int i = 0; i < len; i++) {
if (i != array.length && i != 0) {
int grade1 = Integer.parseInt(array[i].getGrade());
int grade2 = Integer.parseInt(array[i - 1].getGrade());
if (grade1 == grade2) {
array[i].setRanking(array[i - 1].getRanking());
} else {
array[i].setRanking(i + 1 + "");
}
} else {
array[i].setRanking(1 + "");
}
}
return array;
}
}
import java.util.ArrayList;
import java.util.List;
public class JobDemo {
public static void main(String[] args) {
Job[] array = TestSort.mySort(init());
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println(" ======================== ");
Job[] jobArray = TestSort.paixu(array);
for (int i = 0; i < jobArray.length; i++) {
System.out.println(jobArray[i]);
}
}
private static List<Job> init() {
List<Job> list = new ArrayList<Job>();
Job job = null;
job = new Job();
job.setNo(" 11 ");
job.setName(" test ");
job.setGrade("67");
list.add(job);
job = new Job();
job.setNo(" 22 ");
job.setName(" test ");
job.setGrade("67");
list.add(job);
job = new Job();
job.setNo(" 33 ");
job.setName(" test ");
job.setGrade("78");
list.add(job);
job = new Job();
job.setNo(" 44 ");
job.setName(" test ");
job.setGrade("55");
list.add(job);
job = new Job();
job.setNo(" 55 ");
job.setName(" test ");
job.setGrade("99");
list.add(job);
job = new Job();
job.setNo(" 66 ");
job.setName(" test ");
job.setGrade("78");
list.add(job);
job = new Job();
job.setNo(" 77 ");
job.setName(" test ");
job.setGrade("55");
list.add(job);
return list;
}
}