2

I have the following JSON and I'm wondering if it's possible to do a multiple OrderBy using Linq.

var body = @"[{                            
        ""portOfLoading"": ""GOT"",
        ""bookingResponses"":[{
                ""bookingNumber"": ""11"",
                ""comment"": ""LOFO"",
                ""customerReference"": ""3423462"",
                ""departureDate"": ""2017-04-10"",
                ""departureTime"": ""18:00"",
                ""description"": ""desc"",
                ""length"": ""7482"",
                ""netWeight"": ""12345"",
                ""plugin"": ""true"",
                ""resourceCode"": ""CONT26"",
                ""route"": ""GOTZEE"",
                ""status"": ""Price missing"",
                ""unitNumber"": ""ABC123"",
                ""width"": ""0""
            }
        ]
    }
      ,
        {

        ""portOfLoading"": ""GOT"",
        ""bookingResponses"":[{
                ""bookingNumber"": ""3"",
                ""comment"": ""LOFO"",
                ""customerReference"": ""3423462"",
                ""departureDate"": ""2017-04-10"",
                ""departureTime"": ""18:00"",
                ""description"": ""desc"",
                ""length"": ""7482"",
                ""netWeight"": ""12345"",
                ""plugin"": ""true"",
                ""resourceCode"": ""CONT26"",
                ""route"": ""GOTZEE"",
                ""status"": ""Price missing"",
                ""unitNumber"": ""ABC123"",
                ""width"": ""0""
            }
        ]
    }
      ,{
        ""portOfLoading"": ""OUL"",
        ""bookingResponses"":[{
                ""bookingNumber"": ""7"",
                ""comment"": ""STANDBY"",
                ""customerReference"": ""3423462"",
                ""departureDate"": ""2017-04-10"",
                ""departureTime"": ""18:00"",
                ""description"": ""desc"",
                ""length"": ""7482"",
                ""netWeight"": ""12345"",
                ""plugin"": ""true"",
                ""resourceCode"": ""CONT26"",
                ""route"": ""OULZEE"",
                ""status"": ""Price missing"",
                ""unitNumber"": ""ABC123"",
                ""width"": ""0""
            }
            ]
        },{
        ""portOfLoading"": ""ZEE"",
        ""bookingResponses"":[{
                ""bookingNumber"": ""3"",
                ""comment"": ""STANDBY"",
                ""customerReference"": ""3423462"",
                ""departureDate"": ""2017-04-10"",
                ""departureTime"": ""18:00"",
                ""description"": ""desc"",
                ""length"": ""7482"",
                ""netWeight"": ""12345"",
                ""plugin"": ""true"",
                ""resourceCode"": ""CONT26"",
                ""route"": ""ZEEGOT"",
                ""status"": ""Price missing"",
                ""unitNumber"": ""ABC123"",
                ""width"": ""0""
            }
            ]
        },{
        ""portOfLoading"": ""GOT"",
        ""bookingResponses"":[{
                ""bookingNumber"": ""10"",
                ""comment"": ""STANDBY"",
                ""customerReference"": ""3423462"",
                ""departureDate"": ""2017-04-10"",
                ""departureTime"": ""18:00"",
                ""description"": ""desc"",
                ""length"": ""7482"",
                ""netWeight"": ""12345"",
                ""plugin"": ""true"",
                ""resourceCode"": ""CONT26"",
                ""route"": ""GOTZEE"",
                ""status"": ""Price missing"",
                ""unitNumber"": ""ABC123"",
                ""width"": ""0""
            }
            ]
        }
    ]";

So far I've got the 'first' orderby to work, like this:

JArray jsonVal = JArray.Parse(body);
JArray sortQuery = new JArray(jsonVal.OrderBy(obj => obj["portOfLoading"]));

After "portOfLoading" I want to orderBy "bookingNumber". I've tried using ThenBy and so on but never got it to work. Thanks

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Martin Forsell
  • 93
  • 1
  • 2
  • 13

1 Answers1

3

If "bookingResponses" always has a single item in it as in your example do the following:

JArray jsonVal = JArray.Parse(body);
JArray sortQuery = new JArray(jsonVal.OrderBy(obj => obj["portOfLoading"])
                                     .ThenBy(obj => int.Parse(obj["bookingResponses"].FirstOrDefault()?["bookingNumber"].ToString())));

Reason for adding the Int.Parse is because without it the "bookingNumber" will be ordered by the textual ordering of it (as strings) instead of numeric ordering. Leading to an order of: 1,10,11,3. If it is not certain that the values are always a valid integer (and thus cause an InvalidCastException) one can do something like in this answer

Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95