当前位置: 首页 > 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协议支持
  • 上海天文馆走进徐家汇书院,XR沉浸式天文科普体验再推力作
  • 美联储官员:美国经济增速可能放缓,现行关税政策仍将导致物价上涨
  • 董军在第六届联合国维和部长级会议上作大会发言
  • 俄方代表团抵达土耳其,俄乌直接谈判有望于当地时间上午重启
  • Offer触手可及,2025上海社会组织联合招聘专场活动正寻找发光的你
  • 西北大学副校长成陕西首富?旗下巨子生物去年净利超20亿,到底持股多少