当前位置: 首页 > news >正文

多条件排序(C# and Lua)

C#

升序排序

OrderBy

按升序对序列的元素进行排序

ThenBy

按升序对序列中的元素执行后续排序

降序排序

OrderByDescending

按降序对序列的元素排序

ThenByDescending

按降序对序列中的元素执行后续排序

public class Fruit
{
    public int id;
    public string name;
    public float price;
    public float weight;
}

List<Fruit> list = new List<Fruit>();
list.Add(new Fruit { id = 2, name = "苹果", price = 9.9f ,weight = 500});
list.Add(new Fruit { id = 5, name = "西瓜", price = 1.2f, weight = 5000 });
list.Add(new Fruit { id = 1, name = "草莓", price = 29.9f, weight = 80 });
list.Add(new Fruit { id = 3, name = "香蕉", price = 6f, weight = 800 });
list.Add(new Fruit { id = 4, name = "哈密瓜", price = 2.5f, weight = 3000 });

升序排序

IOrderedEnumerable<Fruit> result = list.OrderBy(x => x.id)
    .ThenBy(x=>x.price)
    .ThenBy(x=>x.weight);

降序排序

IOrderedEnumerable<Fruit> result = list.OrderByDescending(x => x.id)
    .ThenByDescending(x=>x.price)
    .ThenByDescending(x=>x.weight);

Lua

local list = {
  [1] = {id = 101,name = "小明",age = 16,score = 88};
  [2] = {id = 103,name = "小红",age = 15,score = 91};
  [3] = {id = 107,name = "小刚",age = 16,score = 81};
  [4] = {id = 105,name = "小丽",age = 15,score = 91};
  [5] = {id = 104,name = "小龙",age = 14,score = 88};
}


table.sort(list,function(a,b)
    if a.score == b.score then
      if a.age == b.age then
        return a.id > b.id
      else
        return a.age > b.age
      end  
    else
      return a.score > b.score
    end  
  end)

相关文章:

  • 生成树(STP)协议
  • 基于 Java 和深度学习的图像分类应用实践
  • 大屏设计新纪元:定制视觉盛宴
  • 【WRF模拟】WPS预处理设置生成文件地址
  • XSS-labs(反射型XSS) 靶场 1-13关 通关
  • 图解AUTOSAR_CP_E2E_Library
  • Linux系统——keepalived安装与部署
  • 用 pytorch 从零开始创建大语言模型(一):理解大型语言模型
  • 关于 Redis 缓存一致
  • 定积分与不定积分在概率统计中的应用
  • idea问题(三)pom文件显示删除线
  • C++ - 从零实现Json-Rpc框架-2(服务端模块 客户端模块 框架设计)
  • 从报错到成功:Mermaid 流程图语法避坑指南✨
  • C# HTTP 文件上传、下载服务器
  • 视频剪辑全流程解析:从素材到成片
  • 大模型训练为什么选择交叉熵损失(Cross-Entropy Loss):均方误差(MSE)和交叉熵损失的深入对比
  • springboot实现调用百度ocr实现身份识别+二要素校验
  • python3.13.2安装详细步骤(附安装包)
  • [AI速读]用持续集成(CI)优化芯片验证环境:Jenkins与EDA工具的实战指南
  • <项目> 高并发服务器的HTTP协议支持
  • 西藏阿里地区日土县连发两次地震,分别为4.8级和3.8级
  • 我国首部《人工智能气象应用服务办法》今天发布
  • 中介在网上非法贩婴“一个孩子8.5万元”?丹阳警方介入
  • 金融创新破局记:中小微企业转型背后的金融力量
  • 大家聊中国式现代化|陶希东:打造高水平安全韧性城市,给群众看得见的安全感
  • 程璧“自由生长”,刘卓辉“被旋律牵着走”