java中的排序

 

 Product类

package cn.edu.fhj.day009.ClooectionDemo;

public class Product {

	private String productId;
	private String productName;
	private float price;
	private int num;

	public Product() {

	}

	public Product(String productId, String productName, float price, int num) {
		this.productId = productId;
		this.productName = productName;
		this.price = price;
		this.num = num;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	/**
	 * toString是一个用来拼装对象内部数据成一个字符串的方法 外部调用该方法即可获取对象数据的拼接字符串
	 */
	public String toString() {
		return this.productId + "," + this.productName + "," + this.price + ","
				+ this.num;
	}

}

  

User类

package cn.edu.fhj.day009.ClooectionDemo;

public class User implements Comparable<User> {

	// id ,name ,age ,salary
	private String id;
	private String name;
	private int age;
	private float salary;

	public User() {

	}

	public User(String id, String name, int age, float salary) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

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

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	@Override
	public String toString() {
		return this.id + "," + this.name + "," + this.age + "," + this.salary;
	}

	@Override
	public int compareTo(User other) {
		if (this.age > other.getAge()) {
			return -1;
		}
		if (this.age == other.getAge()) {
			return 0;
		} else {
			return 1;
		}
	}

}

  

 

排序

用来运行的文件

package cn.edu.fhj.day009.ClooectionDemo;

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

public class ColectionTest {
	public static void main(String[] args) {
		ArrayList<User> uList = new ArrayList<>();
		User u1 = new User("1", "a", 1, 2000);
		User u2 = new User("2", "b", 10, 3000);
		User u5 = new User("5", "e", 490, 2300);
		User u3 = new User("3", "c", 100, 1800);
		User u4 = new User("4", "d", 200, 2300);

		User u6 = new User("6", "f", 555, 2300);
		User u7 = new User("7", "g", 666, 2300);
		uList.add(u1);
		uList.add(u7);

		uList.add(u2);

		uList.add(u3);
		uList.add(u5);
		uList.add(u4);
		uList.add(u6);

		Collections.sort(uList);

		for (User item : uList) {
			System.out.println(item.toString());
		}

		Product p1 = new Product("1", "a", 10, 9);
		Product p2 = new Product("2", "b", 20, 9);
		Product p3 = new Product("3", "c", 15, 6);
		Product p4 = new Product("4", "d", 30, 9);

		ArrayList<Product> pList = new ArrayList<>();

		pList.add(p1);
		pList.add(p2);
		pList.add(p3);
		pList.add(p4);
		// 用Collections工具来排序
		// 方式1:传专门的比较器给工具,本方式最通用,想按什么比就按什么比
		Collections.sort(pList, new Comparator<Product>() {

			@Override
			public int compare(Product o1, Product o2) {
				if (o1.getPrice() > o2.getPrice()) {
					return 1;
				}
				return -1;
			}
		});

		for (Product item : pList) {
			System.out.println(item.toString());
		}

	}

}

 

posted @ 2019-01-15 17:33  cerofang  阅读(318)  评论(0编辑  收藏  举报