假设要对类Test按照字段num排序 如下例:import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;class Test implements Comparator { private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int compare(Object o1, Object o2) { if (((Test) o1).getNum() > ((Test) o2).getNum()) return 1; return 0; } }class TestExecute { public static void printList(List<Test> list) { for (Test test : list) System.out.print(test.getNum()+" "); } @SuppressWarnings("unchecked") public static void main(String[] args) { List<Test> list = new ArrayList<Test>(); for (int i = 10; i > 0; --i) { Test test = new Test(); test.setNum(i); list.add(test); } System.out.println("排序前:"); printList(list);
楼主【dong_kai】截止到2008-06-30 10:13:26的历史汇总数据(不包括此帖):
发帖数:43 发帖分:1270
结贴数:26 结贴分:800
未结数:17 未结分:470
结贴率:60.47 % 结分率:62.99 %
楼主加油
arr.add("哈哈");
arr.add("2");
arr.add("1");
arr.add("5");
arr.add("3");
arr.add("4");
Object[] temp = arr.toArray();
Arrays.sort(temp);
arr = (List) Arrays.asList(temp);
System.out.println(arr);
List list = new ArrayList();
//......
Collections.sort(list, new MyComparator());
class MyComparator implements Comparator
{
//这里的o1和o2就是list里任意的两个对象,然后按需求把这个方法填完整就行了
public int compare(Object o1, Object o2)
{
//比较规则
}
}
import java.util.Arrays;
import java.util.List;public class Test1 {
private String a; private String b; public Test1() { } public Test1(String a, String b) {
this.a = a;
this.b = b;
} public String getA() {
return a;
} public void setA(String a) {
this.a = a;
} public String getB() {
return b;
} public void setB(String b) {
this.b = b;
} private List mySort(List arr) { // 交换排序->冒泡排序
Test1 temp = null;
boolean exchange = false;
for (int i = 0; i < arr.size(); i++) {
exchange = false;
for (int j = arr.size() - 2; j >= i; j--) {
if (((Test1) arr.get(j + 1)).getA().compareTo(
((Test1) arr.get(j)).getA()) >= 0) {
temp = (Test1) arr.get(j + 1);
arr.set(j + 1, (Test1) arr.get(j));
arr.set(j, temp);
exchange = true;
}
}
if (!exchange)
break;
}
return arr;
} public static void main(String[] args) {
List arr = new ArrayList();
Test1 test = new Test1("1", "dssags");
arr.add(test);
test = new Test1("3", "dssags");
arr.add(test);
test = new Test1("2", "dssags");
arr.add(test);
test = new Test1("5", "dssags");
arr.add(test);
test = new Test1("4", "dssags");
arr.add(test);
test = new Test1("6", "dssags");
arr.add(test); Test1 operation = new Test1();
arr = operation.mySort(arr);
for (int i = 0; i < arr.size(); i++) {
System.out.println(((Test1) arr.get(i)).getA());
}
}
}
import java.util.Arrays;
import java.util.List;public class Test1 {
private String a; private String b; public Test1() { } public Test1(String a, String b) {
this.a = a;
this.b = b;
} public String getA() {
return a;
} public void setA(String a) {
this.a = a;
} public String getB() {
return b;
} public void setB(String b) {
this.b = b;
} private List mySort(List arr) { // 交换排序->冒泡排序
Test1 temp = null;
boolean exchange = false;
for (int i = 0; i < arr.size(); i++) {
exchange = false;
for (int j = arr.size() - 2; j >= i; j--) {
if (((Test1) arr.get(j + 1)).getA().compareTo(
((Test1) arr.get(j)).getA()) >= 0) {
temp = (Test1) arr.get(j + 1);
arr.set(j + 1, (Test1) arr.get(j));
arr.set(j, temp);
exchange = true;
}
}
if (!exchange)
break;
}
return arr;
} public static void main(String[] args) {
List arr = new ArrayList();
Test1 test = new Test1("1", "dssags");
arr.add(test);
test = new Test1("3", "dssags");
arr.add(test);
test = new Test1("2", "dssags");
arr.add(test);
test = new Test1("5", "dssags");
arr.add(test);
test = new Test1("4", "dssags");
arr.add(test);
test = new Test1("6", "dssags");
arr.add(test); Test1 operation = new Test1();
arr = operation.mySort(arr);
for (int i = 0; i < arr.size(); i++) {
System.out.println(((Test1) arr.get(i)).getA());
}
}
}
那估计一个关键词是不够的,有第二关键词么?否则就是按这两个值进行排序。
不知道是要 true 在前面,还是 false 在前面呢?
如下例:import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;class Test implements Comparator {
private int num; public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public int compare(Object o1, Object o2) {
if (((Test) o1).getNum() > ((Test) o2).getNum())
return 1;
return 0;
}
}class TestExecute {
public static void printList(List<Test> list)
{
for (Test test : list)
System.out.print(test.getNum()+" ");
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Test> list = new ArrayList<Test>();
for (int i = 10; i > 0; --i) {
Test test = new Test();
test.setNum(i);
list.add(test);
} System.out.println("排序前:");
printList(list);
Collections.sort(list, new Test());
System.out.println("\n排序后:");
printList(list);
}
}
public int compare(Object o1, Object o2) {
if((o1 instanceof MyClass) && (o2 instanceof MyClass)){
return ((MyClass)o1).isOK > ((MyClass)o2).isOK);
}
}
});
根据boolean排序
如果有多个true
你说哪个大?
info1.setId(1);
info1.setRoleName("a");
info1.setCareer(1);
info1.setLevel(1);
info1.setRelationType(1);
info1.setOnline(false);
AyyayList里放的是这样的一些对象,现在想根据online字段排序,也就是用true,false排序,true的在前面,false的在后面!
private int id;
private String roleName;
private int relationType;
private int level;
private int career;
private boolean isOnline;
public int getCareer() {
return career;
}
public void setCareer(int career) {
this.career = career;
}
public boolean isOnline() {
return isOnline;
}
public void setOnline(boolean isOnline) {
this.isOnline = isOnline;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getRelationType() {
return relationType;
}
public void setRelationType(int relationType) {
this.relationType = relationType;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
这是对象的结构!
1:
/**
* 比较器 Collections.sort(list, myCompatator);
*
* @param aone
*/
class MyComparator implements Comparator { public int compare(Object arg0, Object arg1) {
YouType cop1 = (YouType) arg0;
YouType cop2 = (YouType) arg1; int flag = cop1.getOrderId().compareTo(cop2.getOrderId()); return flag;
}}2:
MyComparator myCompatator = new MyComparator();
Collections.sort(docsU, myCompatator);// 排序
那么 ArrayList在排序时会自动掉用 上面你实现的compareTo方法进行排序的
List<ShushanRelationInfoPOJO> list = new ArrayList<ShushanRelationInfoPOJO>(); //..............
Collections.sort(list, new Comparator<ShushanRelationInfoPOJO>()
{
public int compare(ShushanRelationInfoPOJO o1, ShushanRelationInfoPOJO o2)
{
boolean onLine1 = o1.isOnline();
boolean onLine2 = o2.isOnline(); if(onLine1 ^ onLine2)
{
return onLine1? -1: 1;
}
else
{
return 0;
}
}
});
http://blog.csdn.net/numen_wlm/archive/2007/08/22/1753802.aspx
*
* @param locationList
* 传过来的List
* @param Brand
* 实体类(换成你的实体类)
* @param getName
* 实体类的name字段(换成你想排序的字段)
* @return resultList 排序完的List
*/
@SuppressWarnings( { "unchecked", "unused" })
public static List<Brand> ComparatorList(final List<Brand> locationList) {
final List<Brand> resultList = new ArrayList<Brand>();
final Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
final String[] arr = {};
final List<String> list = new ArrayList<String>();
// 把List<LocationBean>里的顺序放到List<String>里
for (final Brand b : locationList) {
list.add(b.getName());
}
// 把list转找为String[]进行排序
final String[] strArr = (String[]) list.toArray(arr);
// 排序
Arrays.sort(strArr, cmp);
// 遍历排序后的字符串
for (int i = 0; i < strArr.length; i++) {
// 遍历原始List<LocationBean>
for (int j = 0; j < locationList.size(); j++) {
// 如果原始List<LocationBean>里的名子有和排好序的当前元素相同
if (locationList.get(j).getName().equals(strArr[i])) {
// 把原始List<LocationBean>中的元素按排好的顺序放到一个新的List<LocationBean>里
resultList.add(locationList.get(j));
}
}
}
return resultList;
}
/**
*
* @param locationList
* 传过来的List
* @param Brand
* 实体类(换成你的实体类)
* @param getName
* 实体类的name字段(换成你想排序的字段)
* @return resultList 排序完的List
*/
@SuppressWarnings( { "unchecked", "unused" })
public static List<Brand> ComparatorList(final List<Brand> locationList) {
final List<Brand> resultList = new ArrayList<Brand>();
final Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
final String[] arr = {};
final List<String> list = new ArrayList<String>();
// 把List<LocationBean>里的顺序放到List<String>里
for (final Brand b : locationList) {
list.add(b.getName());
}
// 把list转找为String[]进行排序
final String[] strArr = (String[]) list.toArray(arr);
// 排序
Arrays.sort(strArr, cmp);
// 遍历排序后的字符串
for (int i = 0; i < strArr.length; i++) {
// 遍历原始List<LocationBean>
for (int j = 0; j < locationList.size(); j++) {
// 如果原始List<LocationBean>里的名子有和排好序的当前元素相同
if (locationList.get(j).getName().equals(strArr[i])) {
// 把原始List<LocationBean>中的元素按排好的顺序放到一个新的List<LocationBean>里
resultList.add(locationList.get(j));
}
}
}
return resultList;
}