当前位置:   article > 正文

Java ArrayList sort() 方法实现集合的升序降序_arraylist排序sort()方法根据添加的长度升序排序

arraylist排序sort()方法根据添加的长度升序排序
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * create by 86159 on 2021/1/11
 */
public class ArraySort {
    public static void main(String[] args) {
        // 生命一个集合
        List<Integer> list = new ArrayList<>();
        list.add(7);
        list.add(2);
        list.add(102);
        list.add(367);
        list.add(-28);

        //升序方式一
        list.sort(Comparator.naturalOrder());
        System.out.println(list);


        // 升序方式二
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        });
        System.out.println(list);


        //倒序方式一
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o2,o1);
            }
        });
        System.out.println(list);

        //倒叙方式二:lamda表达式
        list.sort((x,y) -> Integer.compare(y,x));
        System.out.println(list);
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

运行结果如下:

[-28, 2, 7, 102, 367]
[-28, 2, 7, 102, 367]
[367, 102, 7, 2, -28]
[367, 102, 7, 2, -28]
  • 1
  • 2
  • 3
  • 4

想想,如果一个对象数组,要根据对象中的某一个属性对数组进行排序,该怎么处理呢?

package org.sang.model.entity;

/**
 * @Author: chuxia0811
 * @Date: 2021/1/18 22:37
 * @Description :
 */
public class Student {
    private String sno; //学号
    private String name;//姓名
    private Integer age; //年龄

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno='" + sno + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String sno, String name, Integer age) {
        this.sno = sno;
        this.name = name;
        this.age = age;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
package org.sang.test;

import org.sang.model.entity.Student;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author: chuxia0811
 * @Date: 2021/1/18 22:39
 * @Description :
 */
public class ArraySort2 {
    public static void main(String[] args) {

        Student stu1 = new Student("001", "张三", 18);
        Student stu2 = new Student("002", "李四", 23);
        Student stu3 = new Student("003", "王五", 16);
        Student stu4 = new Student("004", "Jhon", 32);
        Student stu5 = new Student("005", "tom", 10);
        Student stu6 = new Student("006", "alice", 53);

        Student[] stus = new Student[6];
        stus[0] =  stu1;
        stus[1] =  stu2;
        stus[2] =  stu3;
        stus[3] =  stu4;
        stus[4] =  stu5;
        stus[5] =  stu6;
        System.out.println("---------排序前:");
        for (int i = 0;i<stus.length;i++){
            System.out.println(stus[i].toString());
        }
        //排序
        Arrays.sort(stus, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge().compareTo(o2.getAge());//根据age属性正序排序
            }
        });
        System.out.println("---------排序后:");
        for (int i = 0;i<stus.length;i++){
            System.out.println(stus[i].toString());
        }
    }



}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

数组中是student 对象,现在要根据age属性对数组进行排序,运行结果如下:

---------排序前:
Student{sno='001', name='张三', age=18}
Student{sno='002', name='李四', age=23}
Student{sno='003', name='王五', age=16}
Student{sno='004', name='Jhon', age=32}
Student{sno='005', name='tom', age=10}
Student{sno='006', name='alice', age=53}
---------排序后:
Student{sno='005', name='tom', age=10}
Student{sno='003', name='王五', age=16}
Student{sno='001', name='张三', age=18}
Student{sno='002', name='李四', age=23}
Student{sno='004', name='Jhon', age=32}
Student{sno='006', name='alice', age=53}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/509276
推荐阅读
相关标签
  

闽ICP备14008679号