当前位置: 首页 > 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)

http://www.dtcms.com/a/82590.html

相关文章:

  • 生成树(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协议支持
  • 学习使用 Git 和 GitHub 开发项目的教程推荐
  • 浏览器工作原理深度解析(阶段二):HTML 解析与 DOM 树构建
  • vue3中如何缓存路由组件
  • 【QA】装饰模式在Qt中有哪些运用?
  • Python 用户账户(让用户能够输入数据)
  • 【如何打包docker大镜像】
  • NAT和VPN的联系
  • C语言数据结构:栈的操作实现
  • 【从零开始学习计算机科学】软件测试(十)嵌入式系统测试、游戏开发与测试过程、移动应用软件测试 与 云应用软件测试
  • 星越L_灯光操作使用讲解