0

Hi i have been pondering the sorting of string, using linq, which is in the format of

L-NN-L-NN-L
L= A->Z (for all three placements)
NN = 1->99 (first NN)
NN = 1->99 (second NN group by odds then evens)

The code i have so far has brought me to the of running a foreach loop on the first L and adding to a List the sorted arrangement, based on the remaining NN-L-NN-L structure. It would be nice if the sorting of the NN-L-NN-L was a one-liner

any thoughts on this? |Thanks

  • 1
    not understand what you are talking about – Lei Yang May 11 '17 at 03:15
  • I believe you are looking for some variation of natural sort - linked http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp as duplicate covers many ways to perform such sort. If you are looking for something else - make sure to [edit] post to provide good small sample, code you've tried and clear criteria of what you are looking for. (obviously any C# code can be written as one single line - so I assume "one-liner" means something different for you) – Alexei Levenkov May 11 '17 at 03:24
  • whats wrong with list.Sort(StringComparer.Ordinal)? – Keith Nicholas May 11 '17 at 03:54
  • or is your NN 1 2 10, not 01 02 10 – Keith Nicholas May 11 '17 at 03:57
  • here are some examples: 90-A-4-C, 90-A-5-C, 10-D-7-V, 10-G-19-A, 10-G-20-A, 30-W-1-K --- the right arrangement should be: 10-D-7-V, 10-G-19-A, 10-G-20-A , 30-W-1-K, 90-A-5-C, 90-A-4-C because first set needs be sorted 1->99, then sort second set a->z, then sort on third set odd then even 1->99 and finally third set sorted by a->Z. hopefully this explains better... Also, no 01,02 but 1,2. maybe like: `var result90 = List90.OrderByDescending(a => a.l1).ThenByDescending(a => a.l2).ThenBy(a => a.l3% 2 == 1).ThenByDescending(a => a.l4); } ` – user3930668 May 11 '17 at 04:29
  • got it working: var result60 = List60.OrderBy(a => a.l1).ThenByDescending(a => a.l2).ThenByDescending(a => a.l3 % 2).ThenByDescending(a => a.l3 % 2 == 0 ? a.l3 : -a.l3).ThenByDescending(a => a.l4).ToList(); – user3930668 May 11 '17 at 22:23

0 Answers0