0

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

10
  • FYI There is no datetime in your items, only time. Just saying... Commented Feb 12, 2018 at 3:40
  • yeah i converted the datetime to time. sorry i didnt state that Commented Feb 12, 2018 at 3:41
  • So, is this a array of objects or just an array of string Commented Feb 12, 2018 at 3:42
  • 2
    Something like this? hallDetailsList[0].Sort( (x,y) => DateTime.Compare(x.Split('|')[1], y.Split('|')[1] ) ) only problem is you need to convert the time strings back into DateTime to use DateTime.Compare Commented Feb 12, 2018 at 3:45
  • 1
    Enumerable.OrderBy is probably what @Minijack was going for: msdn.microsoft.com/en-us/library/bb534966(v=vs.110).aspx It does take a Func<,> as an argument. Commented Feb 12, 2018 at 3:52

1 Answer 1

1

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]; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.