Implementing PHP’s KSORT in Dynamics NAV

This is just an undocumented snipped of my KSORT (php) implementation in C/AL using .NET. Maybe someone of you also has the requirement to implement this sorting algorithm. I had to use it in combination with a HMAC calculation for a REST request call. Yes, this doesn’t cover all functions KSORT offers, but it might lead one or the other to the right direction.

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

PHP: ksort – Manual
LOCAL KSort(VAR JsonObject : DotNet "Newtonsoft.Json.Linq.JObject";SortNested : Boolean;IsResponseObject : Boolean)
//PHP's KSORT in Dynamics NAV
//Initialize sorted list
SortedList := SortedList.SortedList;

//Read unsorted json object
JSONMgt.InitializeObjectFromJObject(JsonObject);

//Add key/values to sorted list
IF JSONMgt.ReadProperties THEN
  WHILE JSONMgt.GetNextProperty(Key, Value) DO BEGIN
    SortedList.Add(Key, Value);
  END;

//Reinitialize json object
JSONMgt.InitializeEmptyObject;
JSONMgt.GetJSONObject(JsonObject);

//Iterrate through properties
FOREACH KeyValue IN SortedList DO BEGIN
  
  //Check and sort nested objects. In case object is a reponse json, 
  //we don't sort the nested objects, but format it using FormatCrefoJson().
  //Using TryFunctions to determine object type, so care on debugging (errors will be shown)
  IF SortNested THEN BEGIN
    CASE TRUE OF      
      JSONMgt.TryParseJObjectFromString(TmpJObject[1], KeyValue.Value):
        BEGIN
          //Add object to json
          IF IsResponseObject THEN
            JSONMgt.AddJObjectToJObject(JsonObject, KeyValue.Key, FormatCrefoJson(TmpJObject[1].ToString))
          ELSE BEGIN
            //KSort nested object
            KSort(TmpJObject[1], TRUE, IsResponseObject);
            JSONMgt.AddJObjectToJObject(JsonObject, KeyValue.Key, TmpJObject[1]);
          END;
        END;
      JSONMgt.TryParseJArrayFromString(TmpJArray[1], KeyValue.Value):
        BEGIN
          //Create new, sorted array
          JSONMgt.InitializeEmptyCollection;
          JSONMgt.GetJsonArray(TmpJArray[2]);
          
          //Iterate objects in array
          FOREACH TmpJObject[2] IN TmpJArray[1] DO BEGIN
            //KSort nested object and add it to array
            KSort(TmpJObject[2], TRUE, IsResponseObject);
            JSONMgt.AddJObjectToJArray(TmpJArray[2], TmpJObject[2]);
          END;
    
          //Add array to base json object
          JSONMgt.AddJArrayToJObject(JsonObject, KeyValue.Key, TmpJArray[2]);   
        END;
      ELSE 
        JSONMgt.AddJPropertyToJObject(JsonObject, KeyValue.Key, KeyValue.Value);
    END;
  END ELSE
    JSONMgt.AddJPropertyToJObject(JsonObject, KeyValue.Key, KeyValue.Value);
END;
NameTypeSubtype
JSONMgtCodeunitJSON Management
KeyText
ValueText
KeyValueDotNetSystem.Collections.Generic.KeyValuePair`2
ListDotNetSystem.Collections.Generic.List`1
SortedListDotNetSystem.Collections.SortedList
ComparerDotNetSystem.Collections.IComparer
TmpJObjectDotNetNewtonsoft.Json.Linq.JObject
TmpJArrayDotNetNewtonsoft.Json.Linq.JArray.
StringBuilderDotNetSystem.Text.StringBuilder

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published.