c# - How do I sort a custom array list by the dateTime elements -


i have arraylist instances of class (in case 'loan'). loan class contains variable 'date'. want sort arraylist these date variables in optimum way.

the way have done date sorting before, use list<datetime>. in case want sort list/arraylist, keeping rest of information, information can used elsewhere

what slaks said sort in place in arraylist need custom icomparer implementation, put sample below:

public class loan {     public datetime date { get; set; }  }  public class loancomparer : icomparer {     public int compare(object x, object y)     {         loan loanx = x loan;         loan loany = y loan;          return loanx.date.compareto(loany.date);     } }   static void main(string[] args) {     loan l1 = new loan() {date = datetime.now};     loan l2 = new loan() { date = datetime.now.adddays(-5) };      arraylist loans = new arraylist();     loans.add(l1);     loans.add(l2);       loans.sort(new loancomparer()); } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -