排序listjava
A. java 中 List 怎么排序呢
Map<String, String> map =new HashMap<String, String>();
java.util.List<String> list=new ArrayList<String>();
map.put("a", "a");
map.put("c", "c");
map.put("b", "b");
list.addAll(map.keySet());
Collections.sort(list);
for (String key : list)
{
System.out.println(map.get(key));
}
直接Copy 到main方法中运行, 是个降序! 升序的话 Collections 这里面也有的。
楼主 太抠门, 5分都不给!
B. java怎么对list进行排序
1,使用Comparator 接口
Student类 结构如下:(省略getter,setter方法)
public class Student {
/***
* 姓名
*/
private String name;
private int age;
private String address;
/***
* 考试得分
*/
private int score;
//省略getter,setter方法
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score
+ "]";
}
}
测试方法:
@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>();
Student stu=null;
stu=new Student();
stu.setName("whuang");
stu.setAge(12);
stu.setScore(80);
students.add(stu);
stu=new Student();
stu.setName("rong");
stu.setAge(11);
stu.setScore(90);
students.add(stu);
stu=new Student();
stu.setName("zhu");
stu.setAge(15);
stu.setScore(100);
students.add(stu);
Collections.sort(students,new SystemHWUtil. ListComparator(true,"age"));
System.out.println(students);
}
运行结果:
[Student [name=rong, age=11, score=90], Student [name=whuang, age=12, score=80], Student [name=zhu, age=15, score=100]]
核心类:
public static class ListComparator implements Comparator{
/***
* 是否转化为Int之后再比较
*/
private boolean isConvertInteger;
/***
* 对哪个列进行排序
*/
private String comparedProperty;
public ListComparator(boolean isConvertInteger,String comparedProperty) {
super();
this.isConvertInteger = isConvertInteger;
this.comparedProperty=comparedProperty;
}
public int compare(Object o1, Object o2) {
if(null!=o1&&null!=o2)
{
try {
Object obj1=ReflectHWUtils.getObjectValue(o1, comparedProperty);
Object obj2=ReflectHWUtils.getObjectValue(o2, comparedProperty);
if(isConvertInteger){
int num1;
int num2;
if(obj1 instanceof Integer){
num1=(Integer)obj1;
num2=(Integer)obj2;
}else{
num1=Integer.parseInt(obj1.toString());
num2=Integer.parseInt(obj2.toString());
}
if(num1>num2){
return 1;
}else if(num1<num2){
return -1;
}else{
return 0;
}
}else{
return obj1.toString().compareTo(obj2.toString());
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return 0/*等于*/;
}
}
2,可以指定是升序还是降序
实例:
@Test
public void test_ListComparator(){
List<Student>students=new ArrayList<Student>();
Student stu=null;
stu=new Student();
stu.setName("whuang");
stu.setAge(12);
stu.setScore(80);
students.add(stu);
stu=new Student();
stu.setName("rong");
stu.setAge(11);
stu.setScore(90);
students.add(stu);
stu=new Student();
stu.setName("zhu");
stu.setAge(15);
stu.setScore(100);
students.add(stu);
SortList<Student> sortList = new SortList<Student>();
sortList.Sort(students, "getAge", "asc");
System.out.println(students);
}
注意:sortList.Sort 的第二个参数是方法名,不是成员变量名.
核心代码
package com.common.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortList<E> {
public void Sort(List<E> list, final String method, final String sort) {
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Method m1 = ((E) a).getClass().getMethod(method, null);
Method m2 = ((E) b).getClass().getMethod(method, null);
if (sort != null && "desc".equals(sort))// 倒序
ret = m2.invoke(((E) b), null).toString()
.compareTo(m1.invoke(((E) a), null).toString());
else
// 正序
ret = m1.invoke(((E) a), null).toString()
.compareTo(m2.invoke(((E) b), null).toString());
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}
return ret;
}
});
}
}
C. java 中 List<T>如何按照T中的一个字段排序
可以通过以下工具类进行实现:
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* List按照指定字段排序工具类
*
* @param <T>
*/
public class ListSortUtil<T> {
/**
* @param targetList 目标排序List
* @param sortField 排序字段(实体类属性名)
* @param sortMode 排序方式(asc or desc)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void sort(List<T> targetList, final String sortField, final String sortMode) {
Collections.sort(targetList, new Comparator() {
public int compare(Object obj1, Object obj2) {
int retVal = 0;
try {
//首字母转大写
String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w","");
String methodStr="get"+newStr;
Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);
Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);
if (sortMode != null && "desc".equals(sortMode)) {
retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序
} else {
retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序
}
} catch (Exception e) {
throw new RuntimeException();
}
return retVal;
}
});
}
}
Collections.sort(list.);//升序
D. java中list排序
最好是在将日期插入list前排序,即在sql查询是排序,这样比较好处理。
另一种方法是构建一个比较类或比较方法,如一楼所述。
E. JAVA中list集合的排序
根据字符串的含义,进行对象化,比如,Student,有三个属性,序号,姓名,分数
注意重写Student的Compareable接口
然后,List<String>变成List<Student> students=new ArrayList<Student>
然后,遍历list,算出平均分,放入新的SortList<Student>
打印结果
F. Java的List怎么排序啊
问题在于
doubletemp=0.0;
你把temp设置成double型,然后
temp=arr.get(l).doubleValue();
所以你把取得的int类型转换成了double类型,最后输出就带上了.0
你仔细看看你的程序,int类型有的排序后没有变,有的变x.0了,凡是加上.0的都是它在排序过程中与double类型的数字进行过交换。
G. java 怎么将List里面数据排序
学生实体类,包含姓名和年龄属性,
比较时先按姓名升序排序,如果姓名相同则按年龄升序排序。
H. java 怎么将List里面数据排序
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;
publicclassArrayListOrder{
publicstaticvoidmain(String[]args){
List<List<Integer>>list=newArrayList<List<Integer>>();
List<Integer>arrayList1=newArrayList<Integer>();
arrayList1.add(2);
arrayList1.add(5);
arrayList1.add(6);
list.add(arrayList1);
List<Integer>arrayList2=newArrayList<Integer>();
arrayList2.add(2);
arrayList2.add(4);
arrayList2.add(6);
list.add(arrayList2);
List<Integer>arrayList3=newArrayList<Integer>();
arrayList3.add(2);
arrayList3.add(6);
arrayList3.add(6);
list.add(arrayList3);
List<Integer>arrayList4=newArrayList<Integer>();
arrayList4.add(2);
arrayList4.add(1);
arrayList4.add(6);
list.add(arrayList4);
for(List<Integer>tmpList:list){
System.out.print(tmpList.get(1)+"");
}
System.out.println("");
//排序
Collections.sort(list,newComparator<List<Integer>>(){
publicintcompare(List<Integer>list1,List<Integer>list2){
//比较每个ArrayList的第二个元素
if(list1.get(1)==list2.get(1)){
return0;
}elseif(list1.get(1)>list2.get(1)){
return1;
}else{
return-1;
}
}
});
for(List<Integer>tmpList:list){
System.out.print(tmpList.get(1)+"");
}
}
}
I. java List 排名
importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.List;
publicclassStudentSortDemo{
publicstaticvoidmain(String[]args){
List<Student>studentList=newArrayList<Student>();
studentList.add(newStudent("小明",85));
studentList.add(newStudent("小花",85));
studentList.add(newStudent("小军",100));
studentList.add(newStudent("小强",70));
studentList.add(newStudent("小红",85));
Comparator<Student>comparator=newComparator<Student>(){
@Override
publicintcompare(Studento1,Studento2){
if(o1.getSorce()<o2.getSorce()){
return1;
}elseif(o1.getSorce()==o2.getSorce()){
return0;
}else{
return-1;
}
}
};
studentList.sort(comparator);
System.out.println(studentList);
for(inti=0,s=studentList.size();i<s;i++){
if(i>0&&studentList.get(i).getSorce()==studentList.get(i-1).getSorce()){
studentList.get(i).setRank(studentList.get(i-1).getRank());
}else{
studentList.get(i).setRank(i+1);
}
}
System.out.println(studentList);
}
}
classStudent{
privateStringname;
privateintsorce;
privateintrank;
publicStudent(Stringname,intsorce){
this.name=name;
this.sorce=sorce;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetSorce(){
returnsorce;
}
publicvoidsetSorce(intsorce){
this.sorce=sorce;
}
publicintgetRank(){
returnrank;
}
publicvoidsetRank(intrank){
this.rank=rank;
}
@Override
publicStringtoString(){
returngetName()+"分数:"+getSorce()+"排名:"+getRank();
}
}
运行结果:
[小军 分数:100 排名:0, 小明 分数:85 排名:0, 小花 分数:85 排名:0, 小红 分数:85 排名:0, 小强 分数:70 排名:0]
[小军 分数:100 排名:1, 小明 分数:85 排名:2, 小花 分数:85 排名:2, 小红 分数:85 排名:2, 小强 分数:70 排名:5]