集合排序练习

在这里插入图片描述

package com.dodoke.word;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Word {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("orange");
		list.add("tomato");
		list.add("apple");
		list.add("litchi");
		list.add("banana");
		
		System.out.println("排序前:");
		for(String s : list) {
			System.out.println(s + "  ");
		}
		
		Collections.sort(list);
		System.out.println("排序后:");
		for(String s : list) {
			System.out.println(s + "  ");
		}
	}
}

在这里插入图片描述

package com.dodoke.students;

public class Students {
	private int stuld;
	private int age;
	private String name;
	

	public int getStuld() {
		return stuld;
	}
	
	public void setStuld(int stuld) {
		this.stuld = stuld;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Students() {
		super();
	}

	public Students(int stuld, int age, String name) {
		super();
		this.stuld = stuld;
		this.age = age;
		this.name = name;
	}

	@Override
	public String toString() {
		String str = "[学号:" + this.getStuld() + ",";
		str += "姓名:" + this.getAge() + ",";
		str += "成绩:" + this.getName() + "]";
		return str;
	}
}


package com.dodoke.students;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentsTest {
	public static void main(String[] args) {
		Students s1 = new Students(40, 20, "peter");
		Students s2 = new Students(28, 5, "angel");
		Students s3 = new Students(35, 18, "tom");
		
		List<Students> studentsList = new ArrayList<Students>();
		studentsList.add(s1);
		studentsList.add(s2);
		studentsList.add(s3);
		
		System.out.println("排序前:");
		for(Students students : studentsList) {
			System.out.println(students);
		}
		
		Collections.sort(studentsList, new NameComparator());
		System.out.println("排序后:");
		for(Students students : studentsList) {
			System.out.println(students);
		}
	}
}


package com.dodoke.students;

import java.util.Comparator;

public class NameComparator implements Comparator<Students>{
	@Override
	public int compare(Students o1, Students o2) {
		String name1 = o1.getName();
		String name2 = o2.getName();
		int n = name1.compareToIgnoreCase(name2);
		return n;
	}
}

在这里插入图片描述

package com.dodoke.employee;

public class Employee implements Comparable<Employee>{
	private String num;
	private String name;
	private double salary;
	
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	public Employee() {
		super();
	}
	
	public Employee(String num, String name, double salary) {
		super();
		this.num = num;
		this.name = name;
		this.salary = salary;
	}
	@Override
	public String toString() {
		String str = "[编号:" + this.getNum() + ",";
		str += "姓名:" + this.getName() + ",";
		str += "工资:" + this.getSalary() + "]";
		return str;
	}
	@Override
	public int compareTo(Employee o) {
		double salary1 = this.getSalary();
		double salary2 = o.getSalary();
		int n = new Double(salary2-salary1).intValue();
		return n;
	}
}

package com.dodoke.employee;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EmployeeTest {
	public static void main(String[] args) {
		Employee e1 = new Employee("emp001", "张三", 1800.0);
		Employee e2 = new Employee("emp002", "李四", 2500.0);
		Employee e3 = new Employee("emp003", "王五", 1600.0);
		
		List<Employee> list = new ArrayList<Employee>();
		list.add(e1);
		list.add(e2);
		list.add(e3);
		
		System.out.println("排序前:");
		for(Employee employee : list) {
			System.out.println(employee);
		}
		
		Collections.sort(list);
		System.out.println("排序后:");
		for(Employee employee : list) {
			System.out.println(employee);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值