42 个解决方案
#1
#2
请大家帮忙解决一下,在线等待!
#3
没人回帖,拜托知道的告诉一下,谢了!
#4
List arr = new ArrayList();
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);
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);
#5
List list = new ArrayList();
//......
Collections.sort(list, new MyComparator());
class MyComparator implements Comparator
{
//这里的o1和o2就是list里任意的两个对象,然后按需求把这个方法填完整就行了
public int compare(Object o1, Object o2)
{
//比较规则
}
}
#6
ls 正解
#7
#8
因为我想看七楼写了什么,所以回此帖
#9
#10
没注意LZ说的某个字段,正确代码如下:
package src.dyy.superSearch;
import java.util.ArrayList;
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());
}
}
}
#11
重写public int compare(Object o1, Object o2)这个方法.
#12
上面的是根据 private String a 排序
#13
正解,也可以让你的对象直接实现Comparable接口,这样就不用多写一个类
#14
5楼说的对~~~~~~~~~~~~~~~~~~~~~~~
#15
import java.util.ArrayList;
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());
}
}
}
#16
我想根据boolean进行排序,这样也可以吗?
#17
根据boolean咋排哦,就两个值
#18
恩,所以才发愁呢.但是要求就是要根据true,false排序!
#19
那估计一个关键词是不够的,有第二关键词么?否则就是按这两个值进行排序。
不知道是要 true 在前面,还是 false 在前面呢?
#20
假设要对类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);
Collections.sort(list, new Test());
System.out.println("\n排序后:");
printList(list);
}
}
#21
Collections.sort(list,new Comparator(){
public int compare(Object o1, Object o2) {
if((o1 instanceof MyClass) && (o2 instanceof MyClass)){
return ((MyClass)o1).isOK > ((MyClass)o2).isOK);
}
}
});
public int compare(Object o1, Object o2) {
if((o1 instanceof MyClass) && (o2 instanceof MyClass)){
return ((MyClass)o1).isOK > ((MyClass)o2).isOK);
}
}
});
#22
才看到
根据boolean排序
如果有多个true
你说哪个大?
#23
所以得有另外的逻辑,true的放在前面,false的放在后面.
#24
代码帖出来!!!!!!!!
#25
不知道该怎么写代码!
#26
你ArrayList要放的是什么东西,把那个对象的代码帖出来,并注明排序规则
#27
支持这个回答!
#28
ShushanRelationInfoPOJO info1 = new ShushanRelationInfoPOJO();
info1.setId(1);
info1.setRoleName("a");
info1.setCareer(1);
info1.setLevel(1);
info1.setRelationType(1);
info1.setOnline(false);
AyyayList里放的是这样的一些对象,现在想根据online字段排序,也就是用true,false排序,true的在前面,false的在后面!
info1.setId(1);
info1.setRoleName("a");
info1.setCareer(1);
info1.setLevel(1);
info1.setRelationType(1);
info1.setOnline(false);
AyyayList里放的是这样的一些对象,现在想根据online字段排序,也就是用true,false排序,true的在前面,false的在后面!
#29
public class ShushanRelationInfoPOJO implements Serializable{
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;
}
}
这是对象的结构!
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;
}
}
这是对象的结构!
#30
如果没有第二排序关键字,同样的是 true 的话就是按照原来的顺序排列,原先在前排序后也是在前的。
#31
什么意思?不太理解!
#32
如果有第二个排序关键字呢?假如我的第二个排序关键字是relationType,int型的,这种情况要怎么做?
#33
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);// 排序
#34
看了这个问题学到了很多,以前也没遇到过这样的问题
#35
ArrayList 是有序的 使用范型 ArrayList<Object> list = new ArrayList<Object>();
然后 Object implements Comparator 接口 实现里面的 compareTo 方法
那么 ArrayList在排序时会自动掉用 上面你实现的compareTo方法进行排序的
然后 Object implements Comparator 接口 实现里面的 compareTo 方法
那么 ArrayList在排序时会自动掉用 上面你实现的compareTo方法进行排序的
#36
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;
}
}
});
#38
顶下
#39
问题已经解决了,谢谢大家!
#40
你好!你是怎么解决的?我也碰到类似的问题
#41
/**
*
* @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;
}
#42
/**
*
* @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;
}
#1
#2
请大家帮忙解决一下,在线等待!
#3
没人回帖,拜托知道的告诉一下,谢了!
#4
List arr = new ArrayList();
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);
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);
#5
List list = new ArrayList();
//......
Collections.sort(list, new MyComparator());
class MyComparator implements Comparator
{
//这里的o1和o2就是list里任意的两个对象,然后按需求把这个方法填完整就行了
public int compare(Object o1, Object o2)
{
//比较规则
}
}
#6
ls 正解
#7
#8
因为我想看七楼写了什么,所以回此帖
#9
#10
没注意LZ说的某个字段,正确代码如下:
package src.dyy.superSearch;
import java.util.ArrayList;
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());
}
}
}
#11
重写public int compare(Object o1, Object o2)这个方法.
#12
上面的是根据 private String a 排序
#13
正解,也可以让你的对象直接实现Comparable接口,这样就不用多写一个类
#14
5楼说的对~~~~~~~~~~~~~~~~~~~~~~~
#15
import java.util.ArrayList;
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());
}
}
}
#16
我想根据boolean进行排序,这样也可以吗?
#17
根据boolean咋排哦,就两个值
#18
恩,所以才发愁呢.但是要求就是要根据true,false排序!
#19
那估计一个关键词是不够的,有第二关键词么?否则就是按这两个值进行排序。
不知道是要 true 在前面,还是 false 在前面呢?
#20
假设要对类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);
Collections.sort(list, new Test());
System.out.println("\n排序后:");
printList(list);
}
}
#21
Collections.sort(list,new Comparator(){
public int compare(Object o1, Object o2) {
if((o1 instanceof MyClass) && (o2 instanceof MyClass)){
return ((MyClass)o1).isOK > ((MyClass)o2).isOK);
}
}
});
public int compare(Object o1, Object o2) {
if((o1 instanceof MyClass) && (o2 instanceof MyClass)){
return ((MyClass)o1).isOK > ((MyClass)o2).isOK);
}
}
});
#22
才看到
根据boolean排序
如果有多个true
你说哪个大?
#23
所以得有另外的逻辑,true的放在前面,false的放在后面.
#24
代码帖出来!!!!!!!!
#25
不知道该怎么写代码!
#26
你ArrayList要放的是什么东西,把那个对象的代码帖出来,并注明排序规则
#27
支持这个回答!
#28
ShushanRelationInfoPOJO info1 = new ShushanRelationInfoPOJO();
info1.setId(1);
info1.setRoleName("a");
info1.setCareer(1);
info1.setLevel(1);
info1.setRelationType(1);
info1.setOnline(false);
AyyayList里放的是这样的一些对象,现在想根据online字段排序,也就是用true,false排序,true的在前面,false的在后面!
info1.setId(1);
info1.setRoleName("a");
info1.setCareer(1);
info1.setLevel(1);
info1.setRelationType(1);
info1.setOnline(false);
AyyayList里放的是这样的一些对象,现在想根据online字段排序,也就是用true,false排序,true的在前面,false的在后面!
#29
public class ShushanRelationInfoPOJO implements Serializable{
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;
}
}
这是对象的结构!
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;
}
}
这是对象的结构!
#30
如果没有第二排序关键字,同样的是 true 的话就是按照原来的顺序排列,原先在前排序后也是在前的。
#31
什么意思?不太理解!
#32
如果有第二个排序关键字呢?假如我的第二个排序关键字是relationType,int型的,这种情况要怎么做?
#33
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);// 排序
#34
看了这个问题学到了很多,以前也没遇到过这样的问题
#35
ArrayList 是有序的 使用范型 ArrayList<Object> list = new ArrayList<Object>();
然后 Object implements Comparator 接口 实现里面的 compareTo 方法
那么 ArrayList在排序时会自动掉用 上面你实现的compareTo方法进行排序的
然后 Object implements Comparator 接口 实现里面的 compareTo 方法
那么 ArrayList在排序时会自动掉用 上面你实现的compareTo方法进行排序的
#36
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;
}
}
});
#37
这个问题请参考本人博客里面的一篇转载的文章,应该能解决你的问题:
http://blog.csdn.net/numen_wlm/archive/2007/08/22/1753802.aspx
http://blog.csdn.net/numen_wlm/archive/2007/08/22/1753802.aspx
#38
顶下
#39
问题已经解决了,谢谢大家!
#40
你好!你是怎么解决的?我也碰到类似的问题
#41
/**
*
* @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;
}
#42
/**
*
* @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;
}