「java如何给列表排序」java如何给列表排序数据

博主:adminadmin 2022-12-17 02:06:06 69

本篇文章给大家谈谈java如何给列表排序,以及java如何给列表排序数据对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java 下拉列表排序

//可以用Comparator来实现,见代码

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Test implements Comparator{

public static void main(String args[]) {

List sortList=new ArrayList();

sortList.add("3#");

sortList.add("6#");

sortList.add("8#");

sortList.add("10#");

sortList.add("2#");

sortList.add("4#");

System.out.println("before:"+sortList);

Collections.sort(sortList,new Test());

System.out.println("after:"+sortList);

}

public int compare(Object o1, Object o2) {

return Integer.parseInt(o1.toString().replaceAll("#",""))- Integer.parseInt(o2.toString().replaceAll("#",""));

}

}

Java的List怎么排序啊?

用Collections.sort就可以排序,

里面的排序是默认的按自然顺序排列

也就是1,2,3,4这种

参数要求实现了Comparable的数据才能排序,

如果你自己写的类,你就要实现Comparable接口,然后在接口里面自动生成的方法里面

指定排序方法,一般的String Inteneger类都是实现了这个接口的 不用自己操作的。

你可以取看源代码

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(){

ListStudentstudents=new ArrayListStudent();

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!=o1null!=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(num1num2){

return 1;

}else if(num1num2){

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(){

ListStudentstudents=new ArrayListStudent();

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);

SortListStudent sortList = new SortListStudent();

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 SortListE {

public void Sort(ListE 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;

}

});

}

}

java 怎么将List里面数据排序

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class ArrayListOrder {

public static void main(String[] args) {

ListListInteger list = new ArrayListListInteger();

ListInteger arrayList1 = new ArrayListInteger();

arrayList1.add(2);

arrayList1.add(5);

arrayList1.add(6);

list.add(arrayList1);

ListInteger arrayList2 = new ArrayListInteger();

arrayList2.add(2);

arrayList2.add(4);

arrayList2.add(6);

list.add(arrayList2);

ListInteger arrayList3 = new ArrayListInteger();

arrayList3.add(2);

arrayList3.add(6);

arrayList3.add(6);

list.add(arrayList3);

ListInteger arrayList4 = new ArrayListInteger();

arrayList4.add(2);

arrayList4.add(1);

arrayList4.add(6);

list.add(arrayList4);

for (ListInteger tmpList : list) {

System.out.print(tmpList.get(1)+"   ");

}

System.out.println("");

//排序

Collections.sort(list,new ComparatorListInteger(){

public int compare(ListInteger list1, ListInteger list2) {

//比较每个ArrayList的第二个元素

if(list1.get(1)==list2.get(1)){

return 0;

}else if(list1.get(1)list2.get(1)){

return 1;

}else{

return -1;

}

}

});

for (ListInteger tmpList : list) {

System.out.print(tmpList.get(1)+"   ");

}

}

}

关于java如何给列表排序和java如何给列表排序数据的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-17,除非注明,否则均为首码项目网原创文章,转载请注明出处。