java重载排序_Java 排序总结 - MingjaLee的个人空间 - OSCHINA - 中文开源技术交流社区...

Arrays.sort()

public static void sort(int[] a) Sorts the specified array into ascending numerical order. Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.

Parameters: a - the array to be sorted

code

public class ArrayDemo {

public static void main(String[] args) {

// initializing unsorted int array

int iArr[] = {2, 1, 9, 6, 4};

// let us print all the elements available in list

for (int number : iArr) {

System.out.println("Number = " + number);

}

// sorting array

Arrays.sort(iArr);

// let us print all the elements available in list

System.out.println("The sorted int array is:");

for (int number : iArr) {

System.out.println("Number = " + number);

}

}

}

/*

Output...

Number = 2

Number = 1

Number = 9

Number = 6

Number = 4

The sorted int array is:

Number = 1

Number = 2

Number = 4

Number = 6

Number = 9

*/

Collections.sort()

public static > void sort(List list) Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The specified list must be modifiable, but need not be resizable. Implementation Note: This implementation defers to the List.sort(Comparator) method using the specified list and a null comparator. Type Parameters: T - the class of the objects in the list Parameters: list - the list to be sorted.

code

实现comparable接口

/**

* 根据order对User排序

*/

public class User implements Comparable{

private String name;

private Integer order;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getOrder() {

return order;

}

public void setOrder(Integer order) {

this.order = order;

}

public int compareTo(User arg0) {

return this.getOrder().compareTo(arg0.getOrder());

}

} 测试一下:

public class Test{

public static void main(String[] args) {

User user1 = new User();

user1.setName("a");

user1.setOrder(1);

User user2 = new User();

user2.setName("b");

user2.setOrder(2);

List list = new ArrayList();

//此处add user2再add user1

list.add(user2);

list.add(user1);

Collections.sort(list);

for(User u : list){

System.out.println(u.getName());

}

}

}

Collections.sort重载方法来实现

public class User { //此处无需实现Comparable接口

private String name;

private Integer order;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getOrder() {

return order;

}

public void setOrder(Integer order) {

this.order = order;

}

}

主类中这样写即可(HastSet——>List——>sort进行排序):

public class Test {

public static void main(String[] args) {

User user1 = new User();

user1.setName("a");

user1.setPrice(11);

User user2 = new User();

user2.setName("b");

user2.setPrice(2);

Set Hset = new HashSet();

Hset.add(user2);

Hset.add(user1);

List list = new ArrayList();

list.addAll(Hset);

Collections.sort(list,new Comparator(){

public int compare(User arg0, User arg1) {

return arg0.getPrice().compareTo(arg1.getPrice());

}

});

for(User u : list){

System.out.println(u.getName());

}

}

/*

输出结果如下:

a

b

*/

References

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值