c#Listコレクションソート
12305 ワード
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",1,2,"",0.2m,0.1m),
new TicketRuleTimeInfo("C",1,2,"",0.3m,0.1m),
new TicketRuleTimeInfo("D",1,2,"",0.4m,0.1m),
new TicketRuleTimeInfo("A",1,2,"",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 0;
}
return 1;
}
if (y == null)
{
return -1;
}
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 = 0;
/// <summary>
/// { hour: (DepartTime-XePnrTime) MinTime; System.Math.Abs(DepartTime-XePnrTime) MinTime;}
/// </summary>
public int MinTime
{
get { return _MinTime; }
set { _MinTime = value; }
}
private int _MaxTime = 8760;
/// <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 = 0;
/// <summary>
/// %
/// </summary>
public decimal Rate
{
get { return _Rate; }
set { _Rate = value; }
}
private decimal _LowCharge = 0;
/// <summary>
/// { }
/// </summary>
public decimal LowCharge
{
get { return _LowCharge; }
set { _LowCharge = value; }
}
}