-1

I need to sort the list:

List <Issue> usersissueslist = new List <Issue>();

I want to sort it by the property of a regular Issue:

Issue.startDate

The startDate format is a dateTime , "mm/dd/yyyy HH:mm:ss"

For example :

usersissueslist[0].startDate="10.9.15"    usersissueslist[1].startDate="8.9.15"       usersissueslist[2].startDate="9.9.15" 

After the sort:

usersissueslist[0].startDate="8.9.15"        usersissueslist[1].startDate="9.9.15"        usersissueslist[2].startDate="10.9.15"

I WANT TO TO CHANGE THE ISSUE POSITION IN THE LIST AND NOT THE ISSUE STARTDATE.

tnx.

  • Is `startDate` a DateTime type or a String? – Kaf Sep 09 '15 at 07:58
  • 2
    Don't want to be harsh, but did you even try search for something like *'C# list sort'*, which eventually lead you to [List.Sort Method](https://msdn.microsoft.com/en-US/library/w56d4y5z(v=VS.110).aspx) – sloth Sep 09 '15 at 07:59
  • assuming StartDate is of DateTime type. Try this: List sortedList = usersissueslist.OrderBy(x => x.StartDate).ToList(); – M_Idrees Sep 09 '15 at 08:08

2 Answers2

1

Sorting date saved as string will only work when your date format is year.month.day. If you can't (or do not want) use DateTime variable to store date you will have to write your own comparer for 'Issue' or override Equals method. I'd advice to use proper type for DateTime instead of string.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • The startDate format is a dateTime , "mm/dd/yyyy HH:mm:ss" – ziv eliyahu Sep 09 '15 at 08:04
  • Then you would have to write a special comparer to sort it when you store it as string. But this is pointless work - store you startDate as `DateTime` and this (and possibly future) problems will be gone. I can't think of any reason to store date as string. – gzaxx Sep 09 '15 at 08:08
  • I just told you that it isn't a string hhh So how should i sort it? – ziv eliyahu Sep 09 '15 at 08:10
  • `var sorted = usersissueslist.OrderBy(x => x.StartDate).ToList();` - see this [post](http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object) – gzaxx Sep 09 '15 at 08:12
0

One of the way is using linq, assuming your StartDate property is of type string.

 var sortedList = usersissueslist.OrderBy(item => item.StartDate).ToList();

As suggested by everyone.

D J
  • 6,908
  • 13
  • 43
  • 75