通过排序总结java泛型数组列表

·java根据数组的自然顺序对基本数据类型和String类型的泛型数组(数组也可以,不过这里只介绍泛型数组)有很好的排序功能,如下添加4条字符串,然后通过对泛型数组列表进行相应的操作,分别按照升序和降序输出:
import java.util.ArrayList;
import java.util.Collections;

public class Sort {
public static void main(String args[]) {
ArrayList<String> array = new ArrayList<String>();
array.add("ccccc");
array.add("bbbbb");
array.add("ddddd");
array.add("aaaaa");
Collections.sort(array);
System.out.println("升序排列的结果:");
for(String str:array)
System.out.println(str);
Collections.reverse(array);
System.out.println("降序排列的结果:");
for(String str:array)
System.out.println(str);
}
}
输出结果为:
升序排列的结果:
aaaaa
bbbbb
ccccc
ddddd
降序排列的结果:
ddddd
ccccc
bbbbb
aaaaa
而通过以下指定比较器也是可以实现的:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Sort1 implements Comparator<String> {
private final static int UP = 1;
private final static int DOWN = -1;
private int state;

public Sort1(int state) {
this.state = state;
}

public int compare(String o1, String o2) {
return (state == Sort1.DOWN)? sortDown(o1, o2): sortUp(o1, o2);
}

private int sortUp(String o1, String o2) {
if (o1.compareTo(o2) < 0) {
return -1;
} else if (o1.compareTo(o2) > 0) {
return 1;
} else {
return 0;
}
}

private int sortDown(String o1, String o2) {
if (o1.compareTo(o2) > 0) {
return -1;
} else if (o1.compareTo(o2) < 0) {
return 1;
} else {
return 0;
}
}

public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("ccccc");
array.add("bbbbb");
array.add("ddddd");
array.add("aaaaa");
//sort(List<T> list, Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序
Collections.sort(array, new Sort1(Sort1.UP));
System.out.println("升序排列的结果:");
for (String str : array) {
System.out.println(str);
}

Collections.sort(array, new Sort1(Sort1.DOWN));
System.out.println("降序排列的结果:");
for (String str : array) {
System.out.println(str);
}
}
}
其输出结果同上。而当我们需要处理的是对象数组(非String)时,数组的自然序列我们是不知道的,所以,需要使用Comparator进行整体排序(关于Comparator请参见jdk-api 1.6),实现Comparator接口,便要实现其中的compare(T o1, T o2) 。以下程序是进行比较名字,按照lastName优先,firstName其次的原则。
import java.util.*;

public class Sort2 {
public static void main(String[] args) {
ArrayList<Name> name = new ArrayList<Name>();
name.add(new Name("John", "Lennon"));
name.add(new Name("Karl", "Marx"));
name.add(new Name("Groucho", "Marx"));
name.add(new Name("Oscar", "Grouch"));
Collections.sort(name, new Name(new String(), new String()));
for (Name i : name)
System.out.println(i.toString());
}
}

class Name implements Comparator<Name> {
public String firstName, lastName;

public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public int compare(Name n1, Name n2) {
int Cmp = n1.lastName.compareTo(n2.lastName);
return (Cmp != 0 ? Cmp : (n2.firstName).compareTo(n1.firstName));
}

public String toString() {
return firstName + " " + lastName;
}
}
输出结果为:
Oscar Grouch
John Lennon
Karl Marx
Groucho Marx
(G<L<M 当M相等时,K<G,所以得此输出)对于这种方式,我觉得应该会有更简洁的方式,尤其是对于
Collections.sort(name, new Name(new String(), new String()));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值