Tell me how to sort this array of strings:

"text ‡ 11: 35"
"other_text ‡ 16: 30"
"other_text1 ‡ 00: 30"
...
"more_other_text ‡ 12: 30"

So that the lines themselves remain the same lines (cannot be changed) by increasing the time at the end of the line, and the lines with the ending 00:** went to the end also sorted in ascending order in their group.
Please describe in words or by example in C #.

After trial and error, the desired grade turned out with the help of A Petrov , this code sorted - as it should.

  private void SortLinksBySessTime() { string[] result = (from s in sessLinks let dt = DateTime.Parse(s.Substring(s.Length - 5)) let dt2 = dt.Hour == 0 ? dt.AddDays(1) : dt orderby dt2 select s) .ToArray(); } 
  • your solution is where? or do you think you are ready to write a solution to the problem? - teran
  • one
    Honestly, hoping :) - Yuri Morozov

3 answers 3

Something like this?:

 var lines = new []{ "text‡11:35", "other_text‡16:30", "other_text1‡00:30", "more_other_text‡12:30" }; var sorted = (from line in lines let time = TimeSpan.Parse(line.Substring(line.IndexOf('‡') + 1)) orderby time descending select line).ToList(); 
  • The types brought me to the wilds. The string is not recognized as a valid TimeSpan. For some reason, neither the TimeSpan, nor the int is given - Yuri Morozov
  • @YuriMorozov, with all the lines in this format or without numbers, for example? Maybe transfer to TimeSpan manually? - Surfin Bird
  • Indeed, all lines have at the end of 00:00, after a special separator (for example, text ‡ 14: 30). No blank lines, null either. It seems that I will have to sit on it for another day :) The examples above do not work, just like my 50+ solutions. - Yuri Morozov
  • @YuriMorozov, try to parse the TimeSpan manually somehow so that you can immediately see what exactly is not parsed. - Surfin Bird

I will add a more optimal option

 items.Select(i=> new {I=i, sort= ПарсингСтроки(I)}).OrderBy (i =>i.sort).Select(i=>iI); 

That is, we first transform it into a class with a sort field, sort it by this floor, and then we get the initial data. Plus that the data will be parsed once. Can check

By the way, this is based on How to mix (randomly rearrange) the elements in the array?

  • In general, OrderBy caches keys automatically. - Surfin Bird
  • @Surfin Bird Thank you! I'll know. - Serginio

I didn’t understand what the problem was with you: with extracting time, with its parsing or with such sorting, so that time 00 was at the end?

Try this.

Method Syntax:

 var result = list.Select(s => { var dt = DateTime.Parse(s.Substring(s.Length - 5)); if (dt.Hour == 0) dt = dt.AddDays(1); return new { s, dt }; }) .OrderBy(a => a.dt) .Select(a => as) .ToList(); 

Query syntax (in this case is simpler and clearer):

 var result = (from s in list let dt = DateTime.Parse(s.Substring(s.Length - 5)) let dt2 = dt.Hour == 0 ? dt.AddDays(1) : dt orderby dt2 select s) .ToList(); 

We take the last five characters and parsim as time. If the hour is 0, then we add one day: as a result, this value becomes more than the others and when sorting by ascending, it will be at the end.

  • Hello Alexander Petrov! Many thanks for the hint and solution! For me, this is the practice of voodoo until I figure it out. I will post my solution (yours corrected) in the top, and thank you again! - Yuri Morozov