C++实现多级排序
实现功能:期末开始4位同学的成绩,按多级排序,排序规则为:按数学从小到大,如果数学相等,则按语文从大到小排列,如果语文相等,则按英语从小到大排列,如果英语相等,则按历史从大到小排烈
#include "stdafx.h"
#include
#include
#include
typedef struct stu
{
char name[];
int math;//数学
int chinese;//语文
int english;//英语
int history;//历史
}Student;
//按照你想排序的方法排序
bool cmp(stu l, stu r)
{
if (l.math < r.math)//数学从小到大
{
return true;
}
else if (l.math == r.math)//如果数学相等
{
if (l.chinese > r.chinese)//按语文从大到小
{
return true;
}
else if (l.chinese == r.chinese)//如果语文相等
{
if (l.english < r.english)//按英语从小到大
{
return true;
}
else if (l.english == r.chinese)//如果英语相等
{
if (l.history > r.history)//按历史从大到小
{
return true;
}
else if (l.history == r.history)
{
return false;
}
}
}
}
return false;
}
using namespace std;
// 小明 73, 80, 86, 79
//小花 77, 78, 60, 90
// 小刚 73, 79, 90, 90
// 小白 77, 78, 40, 89
//排序后
// 小明 73, 80, 86, 79
// 小刚 73, 79, 90, 90
// 小白 77, 78, 40, 89
//小花 77, 78, 60, 90
Student students[] = { { "小明", , , , }
, { "小花", , , , }
, { "小刚", , , , }
, { "小白", , , , } };
int _tmain(int argc, _TCHAR* argv[])
{
cout << "排序前:===========================================" << endl;
for (Student s : students)
{
cout << s.name << ""
<< s.math << ""
<< s.chinese << ""
<< s.english << ""
<< s.history << endl;
}
sort(students, students + , cmp);
cout << "排序后:===========================================" << endl;
for (Student s : students)
{
cout << s.name << ""
<< s.math << ""
<< s.chinese << ""
<< s.english << ""
<< s.history << endl;
}
getchar();
return ;
}
效果如:
C++实现多级排序的更多相关文章
- python自学笔记(七)排序与多级排序
一.sorted内置方法 a = [1,2,3,4] 从大到小(翻转) a = sorted(a,reverse = True) #生成新对象,不会原地修改,需要重新赋值 print a --> ...
- Python中使用operator模块实现对象的多级排序
Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...
- python list 嵌套 dict 按照字典中的单个key进行单级排序 或 按照多个键进行多级排序
student = [{"no": 1,"score": 90},{"no": 2,"score": 90},{&quo ...
- 用 Python 排序数据的多种方法
用 Python 排序数据的多种方法 目录 [Python HOWTOs系列]排序 Python 列表有内置就地排序的方法 list.sort(),此外还有一个内置的 sorted() 函数将一个可迭 ...
- Python的列表排序
Python的列表排序 本文为转载,源地址为:http://blog.csdn.net/horin153/article/details/7076321 在 Python 中, 当需要对一个 list ...
- python几个排序函数 sort sorted argsort
Python中排序常用到的sort .sorted和argsort函数 [摘要:Python中sort 战 sorted函数 一 .先容 sort函数是list列表中的函数,而 sorted能够对li ...
- ElasticSearch(7)-排序
引用自ElaticSearch权威指南 一.排序 相关性排序 默认情况下,结果集会按照相关性进行排序 -- 相关性越高,排名越靠前. 这一章我们会讲述相关性是什么以及它是如何计算的. 在此之前,我们先 ...
- mysql常用基础操作语法(六)--对数据排序和限制结果数量的条件查询【命令行模式】
1.使用order by对查询的结果进行排序,asc升序,desc降序: 也可以在order by后指定多个字段名和排序方式进行多级排序: 2.使用limit限制查询结果的数量: 上图中的0,代表查询 ...
- .Net中集合排序还可以这么玩
背景: public class StockQuantity { public StockQuantity(string status, DateTime dateTime, int quantity ...
随机推荐
- shell编程基础语法
创建文件:touch aaa.sh 把文件变成可执行的命令: chmod +x /Users/dream-mac/Desktop/aaa.sh (这里是文件路径,如果在当前路径下,只需要把文件名写到这 ...
- in 索引失效的问题
先安利一篇博文MySQL的or/in/union与索引优化 简单的in查询 索引失效: 步骤 1.检查建立索引没有 order_status 字段为普通索引的tinyint类型 2.检查是否使用了使索 ...
- git修改远程仓库关联
公司搬移, 作为git仓库的服务器IP地址变了. 本地代码挺多,重新检出太占时间,可以修改一个什么配置让我本地仓库和新的远程仓库建立关联吗, 答案是肯定的! 方法有很多,这里简单介绍几种: 以下均以项 ...
- Spark WordCount的两种方式
Spark WordCount的两种方式. 语言:Java 工具:Idea 项目:Java Maven pom.xml如下: <properties> <spark.version& ...
- php 按月创建日志
public function log($log_string){ //$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."files". ...
- lua_table 学习
lua table (表) Table 的常用操作 local fruits = {“aaa”,”bbb”,”ccc”,”ddd”,”eee”,”fff”,”ggg”} table.co ...
- lua 语言基础
1.数据类型: string(字符串) ·运算符“+.-.*./”等操作字符串,lua会尝试讲字符串转换为数字后操作: ·字符串连接用“..”运算符 ·用“#”来计算字符串的长度(放在字符串前面) · ...
- 在 Vim 中优雅地查找和替换(转)
总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细介绍 Vim 中查找相关的设置和使用方法. 包括查找与替换.查找光标所在词.高亮前景/背景色.切换高亮状态.大小写敏感查找 ...
- Anaconda虚拟环境
创建虚拟环境:conda create -n env_name packages 例:创建名为env1的虚拟环境,并在其中安装numpy,conda create -n env1 numpy. 指定特 ...
- Java-IO流之BufferedReader 和BufferedWriter的使用和原理
BufferedReader和BufferedWriter出现的目的是为了对FileReader以及FileWriter的读写操作进行增强,而怎么增强呢,原理类似于使用StringBuilder,是把 ...

