Operating table: user
table data:
Requirement: We need to rank the sex field by age, with younger ones at the front and older ones at the back.
1. Add a serial number to the query results using sql statements
This method is to use SQL to process the sorted data and add a separate serial number.
// (SELECT @i:=0)j 声明一个变量(注意,如果声明的变量是关键字则会异常),变量初始值为 0,每次让它+1
// 声明一个变量 (@i:=@i+1)"字段”
SELECT (@i:=@i+1)"序号", u.* FROM 表名 u,(SELECT @i:=0)j
Exception declaration method: the rank command cannot be used
SELECT (@i:=@i+1)"序号", u.* FROM user u,(SELECT @i:=0)rank
- Example 1: Target values not sorted
As you can see from here, the serial number is already there, but our sex field has not been processed yet.
- Example 2: Process the target and then add the sequence number
You can see that our sex has been sorted, and we have also used the serial number as the ranking field (this method is relatively simple, there are more advanced ones below)
2. Increase the serial number according to sex, and tie them according to the same sex
select c.* ,ifnull((
select count(*) from
(select * from user order by sex ) as b
where c.sex > b.sex
),0)+1 as rownum from
(select * from user order by sex ) as c
The effect is as follows:
If you want to rank the smaller ones last and the bigger ones first, change it to less than
The effect is as follows:
3. Use collections to receive these data for processing
public static void main(String[] args) {
// BigDecimal类型
ArrayList<User> list = new ArrayList<>();
list.add(new User(1,"myqxin","1234",11,null,null));
list.add(new User(2,"zfwy","4353",12,null,null));
list.add(new User(3,"qyls","6145",17,null,null));
list.add(new User(4,"ly","2342",12,null,null));
list.add(new User(5,"bl","2532",13,null,null));
List<User> users = mergerList(list);
for (User user : users) {
System.err.println(user);
}
}
public static List<User> mergerList(List<User> list){
//list降序排序
List<User> collect = list.stream().sorted(Comparator.comparing(User::getSex)).collect(Collectors.toList());
//翻转list
Collections.reverse(collect);
Integer index=1;
Integer maxScore=0;
for (int i = 0; i < list.size(); i++) {
if (i==0){
collect.get(i).setRanking(index);
maxScore=collect.get(i).getSex();
}else if (Objects.equals(collect.get(i).getSex(),maxScore)){
collect.get(i).setRanking(index);
}else {
index++;
collect.get(i).setRanking(index);
maxScore=collect.get(i).getSex();
}
}
return collect;
}
The effect is as follows:
Compared with the ranking above, the above is a placeholder. If these things are arranged in order, there will not be the same place-holder.
4. Rank a certain attribute value of an object, and tie objects with the same value.
public static List<ExcellentRanking> mergerList(List<ExcellentRanking> list) {
if (list.size() < 1) {
return new ArrayList<ExcellentRanking>();
}
ArrayList<ExcellentRanking> restwo = new ArrayList<>();
// 对需要排名的值做了处理
for (ExcellentRanking airQualityRankingResp : list) {
if (airQualityRankingResp.getValue() == null) {
airQualityRankingResp.setValue(0.0);
restwo.add(airQualityRankingResp);
} else {
restwo.add(airQualityRankingResp);
}
}
//list降序排序
List<ExcellentRanking> collect = restwo.stream().sorted(Comparator.comparing(ExcellentRanking::getValue)).collect(Collectors.toList());
//翻转list
Collections.reverse(collect);
for (ExcellentRanking airQualityRankingResp : restwo) {
int index = 1;
for (ExcellentRanking airQualityRankingResp1 : collect) {
// > 值越小排名越靠前 < 值越大排名越靠前
if (airQualityRankingResp.getValue() < airQualityRankingResp1.getValue()) {
index++;
}
}
airQualityRankingResp.setRanking(index);
}
return restwo;
}
Effect: