ArrayList 排序
2013-07-19 14:51
134 查看
简单说明:
ArrayList是TestBean的集合,根据TestBean类中的属性对ArrayList中TestBean 排序。
方法:使用Comparator比较器接口和Collections类进行排序。
package audition;
public
class TestBean {
public String
key1;
public String
key2;
public String
key3;
public
int value1;
public
int value2;
public
String toString(){
return
key1+" "+key2+" "+key3+" "+value1+"
"+value2;
}
}
import java.util.Comparator;
import audition.TestBean;
public
class ComparatorTest implements
Comparator<TestBean> {
@Override
public
int compare(TestBeano1, TestBean o2) {
if(o2.key1.equals(o1.key1)==false)
return o2.key1.compareTo(o1.key1);
else
if(o2.key2.equals(o1.key2)==false)
return o2.key2.compareTo(o1.key2);
else
if(o2.key3.equals(o1.key3)==false)
return o2.key3.compareTo(o1.key3);
return 0;
}
}
package audition;
import java.util.ArrayList;
import audition.TestBean;
import java.util.Collections;
public
class TestMain {
public ArrayList<TestBean> init(){
ArrayList<TestBean> al = new ArrayList<TestBean>();
for(int i = 0;i<5;i++){
TestBean tb = new TestBean();
tb.key1 =
"1"+i;
tb.key2 =
"2"+i;
tb.key3 =
"3"+i;
al.add(tb);
}
return al;
}
//ArrayList中的bean,根据key1,key2,key3进行排序,key1>key2>key3
public
void sort(ArrayList<TestBean> al_tb){
ComparatorTest com = new ComparatorTest();
Collections.sort(al_tb, com);
}
public
void display(ArrayList<TestBean> al_tb){
for(TestBean tb : al_tb){
System.out.println(tb);
}
}
public
static void main(String[] str){
TestMain tm = new TestMain();
ArrayList<TestBean> al_tb = tm.init();
tm.sort(al_tb);
tm.display(al_tb);
}
}
ArrayList是TestBean的集合,根据TestBean类中的属性对ArrayList中TestBean 排序。
方法:使用Comparator比较器接口和Collections类进行排序。
package audition;
public
class TestBean {
public String
key1;
public String
key2;
public String
key3;
public
int value1;
public
int value2;
public
String toString(){
return
key1+" "+key2+" "+key3+" "+value1+"
"+value2;
}
}
import java.util.Comparator;
import audition.TestBean;
public
class ComparatorTest implements
Comparator<TestBean> {
@Override
public
int compare(TestBeano1, TestBean o2) {
if(o2.key1.equals(o1.key1)==false)
return o2.key1.compareTo(o1.key1);
else
if(o2.key2.equals(o1.key2)==false)
return o2.key2.compareTo(o1.key2);
else
if(o2.key3.equals(o1.key3)==false)
return o2.key3.compareTo(o1.key3);
return 0;
}
}
package audition;
import java.util.ArrayList;
import audition.TestBean;
import java.util.Collections;
public
class TestMain {
public ArrayList<TestBean> init(){
ArrayList<TestBean> al = new ArrayList<TestBean>();
for(int i = 0;i<5;i++){
TestBean tb = new TestBean();
tb.key1 =
"1"+i;
tb.key2 =
"2"+i;
tb.key3 =
"3"+i;
al.add(tb);
}
return al;
}
//ArrayList中的bean,根据key1,key2,key3进行排序,key1>key2>key3
public
void sort(ArrayList<TestBean> al_tb){
ComparatorTest com = new ComparatorTest();
Collections.sort(al_tb, com);
}
public
void display(ArrayList<TestBean> al_tb){
for(TestBean tb : al_tb){
System.out.println(tb);
}
}
public
static void main(String[] str){
TestMain tm = new TestMain();
ArrayList<TestBean> al_tb = tm.init();
tm.sort(al_tb);
tm.display(al_tb);
}
}
相关文章推荐
- JAVA 用ArrayList对对象中某属性排序
- 关于arraylist多个参数排序
- ArrayList以价格排序
- 对AarrayList进行排序
- 使用Java中的ArrayList给所存对象排序问题
- java对ArrayList中的元素自定义排序
- Java根据容器(List,ArrayList)内对象的字段排序
- ArrayList 排序
- ArrayList去除重复数据,排序
- 定制对ArrayList的sort方法的自定义排序
- java ArrayList的排序
- Java对ArrayList进行排序
- Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例
- ArrayList绑定GridView排序问题
- java List<Item> its=new ArrayList<Item>(); Map按value中的某字段排序
- ArrayList 排序Sort()方法扩展
- ArrayList中的数据排序--java对象排序
- Java ArrayList的自定义排序
- 一个关于ArrayList的自定义排序小练习
- Java ArrayList 的不同排序方法