集合排序

概述:本示例实现对象按年龄升序 人气升序排序功能   姓名升序 降序排序功能

package ch02;

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

/**
* @author YaoShiyou 实现对象排序
*
*/
public class Person {
// 姓名
private String name;
// 年龄
private int age;
// 人气
private int hobby;

public Person(String name) {
  this.name = name;
}

public Person(String name, int age) {
  this(name);
  this.age = age;

}

public Person(String name, int age, int hobby) {
  this(name, age);
  this.hobby = hobby;
}

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 int getHobby() {
  return hobby;
}

public void setHobby(int hobby) {
  this.hobby = hobby;
}

public static void main(String[] args) {

  List<Person> list = new ArrayList<Person>();
  list.add(new Person("g1", 18, 122));
  list.add(new Person("g2", 17, 244));
  list.add(new Person("g3", 45, 243));
  list.add(new Person("g4", 9, 67));
  list.add(new Person("g5", 98, 2));
  System.out.println("排序前---");
  for (Person person : list) {
   System.out.println(person.getName() + "/t" + person.getAge() + "/t"
     + person.getHobby());
  }
  // Collections调用sort 方法 参数为
  Collections.sort(list, new AgeComparatorAsc());
  System.out.println("年龄排序后----");
  for (Person person : list) {
   System.out.println(person.getName() + "/t" + person.getAge() + "/t"
     + person.getHobby());
  }
  Collections.sort(list, new hobbyComparatorAsc());
  System.out.println("人气排序后----");
  for (Person person : list) {
   System.out.println(person.getName() + "/t" + person.getAge() + "/t"
     + person.getHobby());
  }
  Collections.sort(list, new NameComparatorAsc());
  System.out.println("姓名升序排序后----");
  for (Person person : list) {
   System.out.println(person.getName() + "/t" + person.getAge() + "/t"
     + person.getHobby());
  }
 
 
  Collections.sort(list, new NameComparatorDesc());
  System.out.println("姓名降序排序后----");
  for (Person person : list) {
   System.out.println(person.getName() + "/t" + person.getAge() + "/t"
     + person.getHobby());
  }
}
}

// 实现比较器接口 采用Age升序排列
class AgeComparatorAsc implements Comparator<Person> {

public int compare(Person p1, Person p2) {
  if (p1.getAge() > p2.getAge()) {
   return 1;
  } else if (p1.getAge() == p2.getAge()) {
   return 0;
  } else {
   return -1;
  }
}
}

class hobbyComparatorAsc implements Comparator<Person> {

public int compare(Person o1, Person o2) {

  if (o1.getHobby() > o2.getHobby()) {
   return 1;
  } else if (o1.getHobby() == o2.getHobby()) {
   return 0;
  } else {
   return -1;
  }

}
}

class NameComparatorAsc implements Comparator<Person> {

public int compare(Person o1, Person o2) {
  return o1.getName().compareTo(o2.getName());
}

}
class NameComparatorDesc implements Comparator<Person> {

public int compare(Person o1, Person o2) {
  return o2.getName().compareTo(o1.getName());
}

}

//输出结果

排序前---
g1 18 122
g2 17 244
g3 45 243
g4 9 67
g5 98 2
年龄排序后----
g4 9 67
g2 17 244
g1 18 122
g3 45 243
g5 98 2
人气排序后----
g5 98 2
g4 9 67
g1 18 122
g3 45 243
g2 17 244
姓名升序排序后----
g1 18 122
g2 17 244
g3 45 243
g4 9 67
g5 98 2
姓名降序排序后----
g5 98 2
g4 9 67
g3 45 243
g2 17 244
g1 18 122

 

并非个人转载于该地址:http://blog.csdn.net/jiangfeng861016/article/details/5632644

转载于:https://my.oschina.net/u/1041709/blog/897790

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值