Search code examples
c#sortingasp.net-core-mvcasp.net-core-7.0

How to sort and group domtree for select option list


ASP.NET Core 7 MVC & C# shopping cart controller gets DPD pickup point list using

  using HttpClient client = new() { BaseAddress = new Uri("http://ftp.dpdbaltics.com") };
  var domTree = await client.GetFromJsonAsync<JsonNode>("PickupParcelShopData.json");

Result is array of objects sorted by parcelShopId property:

[
  {
    "parcelShopId": "EE90001",
    "legacyShopId": "190001",
    "parcelShopType": "PickupStation",
    "companyName": "Automat 1 place xxx",
    "companyShortName": "Automat1",
    "street": "\u00D5ism\u00E4e tee 46",
    "houseNo": "",
    "addressLine2": "London",
    "countryCode": "UK",
    "zipCode": "13512",
    "city": "Tallinn",
    "longitude": "24.64727",
    "latitude": "59.411726",
    "openingHours": [
      {
        "weekday": "Monday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Tuesday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Wednesday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Thursday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Friday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Saturday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      },
      {
        "weekday": "Sunday",
        "openMorning": "0001",
        "closeMorning": "1200",
        "openAfternoon": "1200",
        "closeAfternoon": "2359"
      }
    ]
  },
  {
    "parcelShopId": "EE90002",
    "legacyShopId": "190002",
    "parcelShopType": "PickupStation",
    "companyName": "Automa3 xxx ",
    "companyShortName": "Automa3",
    "street": "Endla 45",
    "houseNo": "",
    "addressLine2": "Riga",
    "countryCode": "EE",
    "zipCode": "10615",
    "city": "Riga",
    "longitude": "24.7227",
    "latitude": "59.42714",
    "openingHours": [
 ....

Razor view renders select element from it using

<select id='dpd_select'>
@foreach ( var esi in domTree.AsArray() ) {
  <option value='@esi["parcelShopId"]'>@esi["companyName"]</option>
}
</select>

It is difficult to find pickup point from this order.

How to order and group by select element option list by 3 properties:

 countryCode
 city
 companyName

Should domTree be sorted and grouped?


Solution

  • you can use this code for a select list

         var jArr = JArray.Parse(json);
    
           var selectList = jArr
              .Select(x => new JObject
              {
                  ["countryCode"] = x["countryCode"],
                  ["city"] = x["city"],
                  ["parcelShopId"] = x["parcelShopId"],
                  ["companyName"] = x["companyName"]
              })
              .OrderBy(x => x["countryCode"])
              .ThenBy(x => x["city"])
              .ThenBy(x => x["companyName"]);
    

    or using system.text.json

     var jArr = JsonArray.Parse(json).AsArray();
    
           var selectList = jArr
              .Select(x => new Dictionary<string, string>
              {
                  ["countryCode"] = (string) x["countryCode"],
                  ["city"] = (string) x["city"],
                  ["parcelShopId"] =  (string) x["parcelShopId"],
                  ["companyName"] =  (string) x["companyName"]
              })
              .OrderBy(x => x["countryCode"])
              .ThenBy(x => x["city"])
              .ThenBy(x => x["companyName"]);