Reputation: 162
I have an list of arraylist List<ArrayList> hallDetailsList
and i am trying to sort them by the time
List Data
hallDetailsList:
[0][0]Movie A|4:10pm|Hall 1
[0][1]Movie A |6:50pm|Hall 1
[0][2]Movie A |9:30pm|Hall 1
[0][3]Movie A |12:10am|Hall 1
[1][0]Movie B|4:40pm|Hall 2
[1][1]Movie B|9:40pm|Hall 2
[1][2]Movie B|12:10am|Hall 2
[1][3]Movie B|7:10pm|Hall 2
[1][4]Movie B|12:00pm|Hall 2
[1][5]Movie B|2:40pm|Hall 2
[1][6]Movie B|8:30pm|Hall 2
[2][0]Movie B|1:00pm|Hall 3
[2][1]Movie C|3:10pm|Hall 3
[2][2]Movie C|9:20pm|Hall 3
[2][3]Movie C|11:30pm|Hall 3
[2][4]Movie C|5:20pm|Hall 3
[2][5]Movie C|7:20pm|Hall 3
[3][0]Movie C|1:00pm|Hall 4
[3][1]Movie C|3:00pm|Hall 4
[3][2]Movie D|3:40pm|Hall 4
[3][3]Movie D|6:30pm|Hall 4
[3][4]Movie D|9:20pm|Hall 4
[3][5]Movie D|12:15am|Hall 4
[3][6]Movie D|5:30pm|Hall 4
[3][7]Movie D|12:30pm|Hall 4
I managed to sort the data through halls as previously they were just jumbled up everywhere. How do i sort the items now through time? as right now in some arraylist, the timing is messed up
Upvotes: 0
Views: 83
Reputation: 81473
Given this
List<ArrayList> hallDetailsList = new List<ArrayList>()
{
new ArrayList()
{
0,
0,
"Movie A",
"1:10pm",
"Hall 1"
},
new ArrayList()
{
0,
0,
"Movie A",
"4:10pm",
"Hall 1"
},
new ArrayList()
{
0,
0,
"Movie A",
"3:10pm",
"Hall 1"
}
};
You can order like this
var results = hallDetailsList.OrderBy(item => DateTime.ParseExact(((string)item[3]), "h:mmtt", null, System.Globalization.DateTimeStyles.None))
.ToList();
Or slightly more readable
var Result= hallDetailsList.OrderBy(item =>
{
var time = (string)item[3];
return DateTime.ParseExact(time, "h:mmtt", null, System.Globalization.DateTimeStyles.None);
}).ToList();
Note : Depending on your actual ArrayList
you may have to change the indexing on
var time = (string)item[3];
Upvotes: 1