ArrayList 객체 정렬 인스턴스
1900 단어 ArrayList
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class SortTwo implements Comparator {
//
private String sort1;
//
private int state;
public SortTwo(int state){
this.state=state;
}
public SortTwo(){
}
public int SortUp(Object o1,Object o2){
Student stu=(Student) o1;
Student stu1=(Student) o2;
if(sort1.equals("name")){
if(stu.getName().compareTo(stu1.getName())>1){
return 1;
} else if(stu.getName().compareTo(stu1.getName())<1){
return -1;
}else{
return 0;
}
}
if(sort1.equals("age")){
if(stu.getAge().compareTo(stu1.getAge())>1){
return 1;
} else if(stu.getAge().compareTo(stu1.getAge())<1){
return -1;
}else{
return 0;
}
}
return 0;
}
public int SortDOWN(Object o1,Object o2){
Student stu=(Student) o1;
Student stu1=(Student) o2;
if(sort1.equals("name")){
if(stu.getName().compareTo(stu1.getName())>1){
return -1;
} else if(stu.getName().compareTo(stu1.getName())<1){
return 1;
}else{
return 0;
}
}
if(sort1.equals("age")){
if(stu.getAge().compareTo(stu1.getAge())>1){
return -1;
} else if(stu.getAge().compareTo(stu1.getAge())<1){
return 1;
}else{
return 0;
}
}
return 0;
}
public int compare(Object o1, Object o2) {
if(this.state==1){
return SortUp(o1, o2);
}else{
return SortDOWN(o1, o2);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student();
stu.setName("123");
stu.setAge("12");
Student stu1=new Student();
stu1.setName("234");
stu1.setAge("23");
ArrayList<Student>list=new ArrayList<Student>();
list.add(stu);
list.add(stu1);
SortTwo t=new SortTwo(1);
t.sort1="age";
Collections.sort(list,t);
for(Student st : list){
System.out.println(st.getName() +" "+st.getAge());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java ArrayList는 동일한 속성으로 그룹화java ArrayList는 동일한 속성으로 그룹화 선언: 일반적으로 SQL을 사용하여 한 무리의 데이터를 조회할 때 SQL의 GROUP BY 문구를 이용하여 데이터를 그룹화할 수 있지만, 때로는 성능에 대한 고려 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.