java 调用排序_Java开发中使用sort排序

刷题过程中常常遇到排序问题,Java中自带的sort方法可以非常方便的帮助我们进行排序。

常见的排序问题有两种情形:

1.对一个数组进行排序。

2.对自定义类型的类进行排序。

一,对数组进行排序:

通常情况下我们可以使用Array.sort()来对数组进行排序,有以下3种情况:

1.Array.sort(int[] a)

直接对数组进行升序排序

2.Array.sort(int[] a , int fromIndex, int toIndex)

对数组的从fromIndex到toIndex进行升序排序

3.新建一个comparator从而实现自定义比较

具体方法如下:

825ade3dac531d9c16f813d922f44a2e.png

34ceeed9e63708f9a1c0ece48a698afc.png

二,对自定义类进行排序

当我们处理自定义类型的排序时,一般将自定义类放在List种,之后再进行排序

一般我们对自定义类型数据进行重写Comparator来进行对数据进行比较

具体方法如下:

7101393e95d281aa74236abd73206669.png

下面来分析两道题目:

1028 List Sorting (25 分)

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.

Sample Input 1:

3 1

000007 James 85

000010 Amy 90

000001 Zoe 60

Sample Output 1:

000001 Zoe 60

000007 James 85

000010 Amy 90

Sample Input 2:

4 2

000007 James 85

000010 Amy 90

000001 Zoe 60

000002 James 98

Sample Output 2:

000010 Amy 90

000002 James 98

000007 James 85

000001 Zoe 60

Sample Input 3:

4 3

000007 James 85

000010 Amy 90

000001 Zoe 60

000002 James 90

Sample Output 3:

000001 Zoe 60

000007 James 85

000002 James 90

000010 Amy 90显然我们新建的类中应该包含ID,name,val(成绩) ,之后我们新建三种比较器便可以完成这三种比较方式了,代码如下:

b3d9e7087b88ba76ac87a413940e2bc0.png

90dce81af04f90e7ab9b1091b53b2240.png

adc91cbce7d453e8a026b5977a46360c.png

再看一道题目:

1025 PAT Ranking (25 分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2

5

1234567890001 95

1234567890005 100

1234567890003 95

1234567890002 77

1234567890004 85

4

1234567890013 65

1234567890011 25

1234567890014 100

1234567890012 85

Sample Output:

9

1234567890005 1 1 1

1234567890014 1 2 1

1234567890001 3 1 2

1234567890003 3 1 2

1234567890004 5 1 4

1234567890012 5 2 2

1234567890002 7 1 5

1234567890013 8 2 3

1234567890011 9 2 4这到题目需要加入两种排序方式——考试地点的排名,以及总排名,所以这里我在每一个考场都建立了一个list进行排序,还有一个总的ranking对所有考生进行排序。代码如下:

35eabdd700d47a91fafcf1767157f1f5.png

c551852f105df675ec2d3568456723a1.png

0e2795b7ea34f814208c59ce6d4e97be.png

4865c625f753e7a5b2e0ad1bb36dbb15.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值