c# List集合排序

时间:2023-03-09 18:30:28
c# List集合排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class ListRate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<TicketRuleTimeInfo> list = new List<TicketRuleTimeInfo>
{
new TicketRuleTimeInfo("B",,,"",0.2m,0.1m),
new TicketRuleTimeInfo("C",,,"",0.3m,0.1m),
new TicketRuleTimeInfo("D",,,"",0.4m,0.1m),
new TicketRuleTimeInfo("A",,,"",0.1m,0.1m),
};
foreach (var item in list)
{
Response.Write(item.DepartBA + ":" + item.Rate);
}
Console.WriteLine("排序后");
list.Sort(CompareByRate);
Response.Write("========================");
foreach (var item in list)
{
Response.Write(item.DepartBA + ":" + item.Rate);
} }
public static int CompareByRate(TicketRuleTimeInfo x, TicketRuleTimeInfo y)//从大到小排序器
{
if (x == null)
{
if (y == null)
{
return ;
} return ; }
if (y == null)
{
return -;
}
int retval = y.Rate.CompareTo(x.Rate);
return retval;
}
}
public class TicketRuleTimeInfo
{
public TicketRuleTimeInfo(string departBA, int minTime, int maxTime, string stadardCabin, decimal rate, decimal lowCharge)
{
DepartBA = departBA;
MinTime = minTime;
MaxTime = maxTime;
StandardCabin = stadardCabin;
Rate = rate;
LowCharge = LowCharge;
}
private string _DepartBA = "before";
/// <summary>
/// 起飞前后{before起飞前、after起飞后}
/// </summary>
public string DepartBA
{
get { return _DepartBA; }
set { _DepartBA = value; }
}
private int _MinTime = ;
/// <summary>
/// 时间区间最低{单位小时hour: 起飞前(DepartTime-XePnrTime)大于MinTime;起飞后System.Math.Abs(DepartTime-XePnrTime)大于MinTime;}
/// </summary>
public int MinTime
{
get { return _MinTime; }
set { _MinTime = value; }
}
private int _MaxTime = ;
/// <summary>
/// 时间区间最高{单位小时hour: 起飞前(DepartTime-XePnrTime)小于等于MaxTime;起飞后System.Math.Abs(DepartTime-XePnrTime)小于等于MaxTime;}
/// </summary>
public int MaxTime
{
get { return _MaxTime; }
set { _MaxTime = value; }
}
private string _StandardCabin = "";
/// <summary>
/// 基准舱位
/// </summary>
public string StandardCabin
{
get { return _StandardCabin; }
set { _StandardCabin = value; }
}
private decimal _Rate = ;
/// <summary>
/// 费率%
/// </summary>
public decimal Rate
{
get { return _Rate; }
set { _Rate = value; }
}
private decimal _LowCharge = ;
/// <summary>
/// 最低手续费{单位元}
/// </summary>
public decimal LowCharge
{
get { return _LowCharge; }
set { _LowCharge = value; }
}
}