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

3
  • What if "bookingResponses" has several items beneath it? what is your expected output? Commented Apr 18, 2017 at 14:15
  • you can use .ThenBy(obj2 => obj2["bookingNumber"] I think. Commented Apr 18, 2017 at 14:21
  • @Achilles you can't, see Gilad's answer for why that's insufficient. Commented Apr 18, 2017 at 14:22

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.