The front-end time has just completed a project and finally has time to summarize and review it.
Project requirements: send emails to users. emails are classified into system emails and personal emails, which need to be sorted by time and unread in descending order.
At first, I thought this was a simple requirement. I created an object for the email:
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 ;}
}
}
Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)
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;
}
}
}
The results can be imagined and are not arranged in descending order of unread.
Method 1: perform in-depth copy of 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;
}
}
}
Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)
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;
}
}
}
The result still does not work. It is depressing.
Finally, the value type (struct) is used for processing:
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 ;}
}
}
Then List <Mail> listMails = new List <Mail> for Bubble Sorting (which has been sorted by time in descending order, and now only needs to be sorted by unread)
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;
}
}
}
Test, all OK, we recommend that all students have a deep understanding of the value type and reference type to avoid unnecessary trouble.
Reprinted, please indicate the source, mobile game base www.shouyou888.com, QQD123 game network www.qqd123.com