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

多条件排序(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.kler.cn/a/595322.html

相关文章:

  • vscode设置console.log的快捷输出方式
  • springboot项目引用外部jar包,linux部署后启动失败,找不到jar包
  • LeetCode[454]四数相加Ⅱ
  • 分布式唯一ID
  • LDAP从入门到实战:环境部署与配置指南(下)
  • 希尔排序中的Hibbard序列
  • 如何在MCU工程中启用HardFault硬错误中断
  • FPGA中串行执行方式之状态机
  • 蓝桥杯 之 数论
  • Spring Boot 集成 Quartz 实现定时任务(Cron 表达式示例)
  • Windows10配置OpenJDK11
  • 基于深度学习的目标追踪技术全解析
  • 验证码背后:前端安全问题的深度剖析
  • 前端网络请求
  • 【强化学习】Reward Model(奖励模型)详细介绍
  • 智能工厂能耗分析:Python驱动的高效能源管理
  • 「0基础学爬虫」爬虫基础之抓包工具的使用
  • SQLite 查询数据库属性
  • AI视频是否会影响原创价值
  • 人工智能:企业RAG方案