前端时间刚刚弄完一个项目,终于有时间来总结与回顾了。
项目需求:给用户发送邮件,邮件分为系统邮件和个人邮件,需要按时间、未读降序排列。
一开始以为,这是一个很简单的需求,给邮件建了一个对象:
class Mail{
private string _title =string.Empty;
public string Title{
get {return _title;}
}
private string _message = string.Empty;
public string Message {
get {return _message;}
}
private bool _isRead = false ;
public bool IsRead {
get {return _isRead}
}
privaite DateTime _insertTime = DateTime.Now;
public DateTime InsertTime{
get {return _insertTime ;}
}
}
然后再 List<Mail> listMails = new List<Mail> 进行冒泡排序(里面已经按时间降序了,现在只需按未读排序)
for(int i=0,count1=listMails.Count;i<count1;i++){
for(int j= i+1;j<count1;j++){
if(listMails[i].IsRead && !listMails[j].IsRead){
Mail temp = listMails[i];
listMails[i] = listMails[j];
listMails[j]=temp;
}
}
}
结果可想而知了,并未按未读降序排列。
改进方法1:对Mail进行深度复制:
[Serializable]
class Mail:ICloneable{
private string _title =string.Empty;
public string Title{
get {return _title;}
}
private string _message = string.Empty;
public string Message {
get {return _message;}
}
private bool _isRead = false ;
public bool IsRead {
get {return _isRead}
}
privaite DateTime _insertTime = DateTime.Now;
public DateTime InsertTime{
get {return _insertTime ;}
}
public object Clone()
{
using (Stream objectStream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, this);
objectStream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(objectStream) as Employee;
}
}
}
然后再 List<Mail> listMails = new List<Mail> 进行冒泡排序(里面已经按时间降序了,现在只需按未读排序)
for(int i=0,count1=listMails.Count;i<count1;i++){
for(int j= i+1;j<count1;j++){
if(listMails[i].IsRead && !listMails[j].IsRead){
Mail temp = (Mail)listMails[i].Clone();
listMails[i] = (Mail)listMails[j].Clone();
listMails[j]=temp;
}
}
}
结果还是不行,郁闷了。
最后采用值类型(struct)进行处理:
struct Mail{
private string _title;
public string Title{
get {return _title;}
}
private string _message;
public string Message {
get {return _message;}
}
private bool _isRead;
public bool IsRead {
get {return _isRead}
}
privaite DateTime _insertTime;
public DateTime InsertTime{
get {return _insertTime ;}
}
}
然后再 List<Mail> listMails = new List<Mail> 进行冒泡排序(里面已经按时间降序了,现在只需按未读排序)
for(int i=0,count1=listMails.Count;i<count1;i++){
for(int j= i+1;j<count1;j++){
if(listMails[i].IsRead && !listMails[j].IsRead){
Mail temp = listMails[i];
listMails[i] = listMails[j];
listMails[j]=temp;
}
}
}
测试,一切OK,建议所有的同学,深入理解值类型和引用类型,避免造成不必要的麻烦。
转载,请注明来源,手游基地 www.shouyou888.com,QQD123游戏网 www.qqd123.com