java中对象的字符串类型属性排序

测试环境:

SpringBoot2.0.3执行测试所需的依赖:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

说明

对学生对象的姓名属性进行排序。

方法一:内部类实现comparator接口

测试代码(正序排序):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudyTest {
    /*
     * Java对对象按照其属性排序的两种方法--使用Comparator匿名内部类实现
     */
    @Test
    public void innerSort(){
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("大铭", 19, 89));
        students.add(new Student("来福", 26, 90));
        students.add(new Student("仓颉", 23, 70));
        students.add(new Student("王磊", 18, 80));

        System.out.println("排序前:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

        Collections.sort(students,new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                return o1.name.compareTo(o2.name);
            }
        });

        System.out.println("排序后:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

    }
    class Student {
        String name;
        int age;
        int score;
        public Student (String name,int age,int score){
            this.age = age;
            this.name = name;
            this.score = score;
        }
    }
}
输出结果(正序排序):
排序前:
姓名:大铭 年龄:19 成绩:89
姓名:来福 年龄:26 成绩:90
姓名:仓颉 年龄:23 成绩:70
姓名:王磊 年龄:18 成绩:80
排序后:
姓名:仓颉 年龄:23 成绩:70
姓名:大铭 年龄:19 成绩:89
姓名:来福 年龄:26 成绩:90
姓名:王磊 年龄:18 成绩:80
测试代码(逆序排序):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudyTest {
    /*
     * Java对对象按照其属性排序的两种方法--使用Comparator匿名内部类实现
     */
    @Test
    public void innerSort(){
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("大铭", 19, 89));
        students.add(new Student("来福", 26, 90));
        students.add(new Student("仓颉", 23, 70));
        students.add(new Student("王磊", 18, 80));

        System.out.println("排序前:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

        Collections.sort(students,new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                if(o1.name.compareTo(o2.name) == -1){
                    return 1;
                } else {
                    return -1;
                }
            }
        });

        System.out.println("排序后:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

    }
    class Student {
        String name;
        int age;
        int score;
        public Student (String name,int age,int score){
            this.age = age;
            this.name = name;
            this.score = score;
        }
    }
}
输出结果(逆序排序):
排序前:
姓名:大铭 年龄:19 成绩:89
姓名:来福 年龄:26 成绩:90
姓名:仓颉 年龄:23 成绩:70
姓名:王磊 年龄:18 成绩:80
排序后:
姓名:王磊 年龄:18 成绩:80
姓名:仓颉 年龄:23 成绩:70
姓名:来福 年龄:26 成绩:90
姓名:大铭 年龄:19 成绩:89

方法二:实现comparable接口

测试代码:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudyTest {
    @Test
    public void impleSort(){
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("大铭", 19, 89));
        students.add(new Student("来福", 26, 90));
        students.add(new Student("仓颉", 23, 70));
        students.add(new Student("王磊", 18, 80));

        System.out.println("排序前:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

        Collections.sort(students);

        System.out.println("排序后:");
        for (Student student : students) {
            System.out.println("姓名:"+student.name+" 年龄:"+student.age+" 成绩:"+student.score);
        }

    }
    class Student implements Comparable<Student> {
        String name;
        int age;
        int score;
        public Student (String name,int age,int score){
            this.age = age;
            this.name = name;
            this.score = score;
        }

        @Override
        public int compareTo(Student o){
             return this.name.compareTo(o.name);
        }

    }
}
输出结果:
排序前:
姓名:大铭 年龄:19 成绩:89
姓名:来福 年龄:26 成绩:90
姓名:仓颉 年龄:23 成绩:70
姓名:王磊 年龄:18 成绩:80
排序后:
姓名:仓颉 年龄:23 成绩:70
姓名:大铭 年龄:19 成绩:89
姓名:来福 年龄:26 成绩:90
姓名:王磊 年龄:18 成绩:80

java中对象的整型类型属性排序参考:https://blog.csdn.net/wangtaocsdn/article/details/71500500

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值