1

I have List<Tuple<string[], SolidColor>> ExcelData as my list. I can order ExcelData with LINQ's functions OrderBy and ThenBy and add EmptyStringsAreLast function to OrderBy which I found from this guy (https://stackoverflow.com/a/14641992/11848085). Code looks like this:

ExcelData = ExcelData
    .OrderBy(arr => arr.Item1[2], new EmptyStringsAreLast())
    .ThenBy(arr => arr.Item1[1])
    .ThenBy(arr => arr.Item1[0])
    .ToList();

That works as long as I don't want to use dynamic OrderBy. If I use dynamic OrderBy, like this one (This one requires System.Linq.Dynamic or System.Linq.Dynamic.Core), I can't possible figure out any way possible to add that EmptyStringsAreLast() function to that first argument (Item1[2] ASC). (https://stackoverflow.com/a/8660293)

ExcelData = ExcelData.AsQueryable()
    .OrderBy("Item1[2] ASC, Item1[1] ASC, Item1[0] ASC")
    .ToList();

Is there a way to do something like this?

ExcelData = ExcelData.AsQueryable()
    .OrderBy("'Item1[2] ASC, new EmptryStringsAreLast()', Item1[1] ASC, Item1[0] ASC")
    .ToList();
Eireannanl
  • 11
  • 1
  • Since `EmptyStringsAreLast()` is not dynamic, can you not do the dynamic part and then apply the `EmptyStringsAreLast()` to the result? – CodingYoshi Jul 28 '19 at 12:05
  • Off topic but I would make a custom type instead of `Tuple` so the code is easier to read. Then I would have `List` instead of `List>` – CodingYoshi Jul 28 '19 at 12:11
  • How do I benefit from `List`, if I use it rathen than this `List>`? Edit. Just read that it's easier to read, I assume that can't be only reason for that. – Eireannanl Jul 28 '19 at 14:59
  • I don't thing that applying `EmptyStringsAreLast()` to result works as in my program, there's 3 columns I want to order by. – Eireannanl Jul 28 '19 at 17:20

1 Answers1

0

Looks like I managed to figure it out. By adding String.IsNullOrWhiteSpace(Item1[2]), it sorts null or empty as last.

Like this one

ExcelData = ExcelData.AsQueryable()
      .OrderBy("String.IsNullOrWhiteSpace(Item1[2]), Item1[2] ASC, Item1[1] ASC, Item1[0] ASC")
      .ToList();
Eireannanl
  • 11
  • 1