skip to Main Content

I have a list of strings present in the below json format and I need to verify if the displayedString in the outer array is in the sorted order with a slight modification to the sorting logic.

{  
  "outer": [ 
            { "inner" : { "displayedString" : "The 40 AOI one"}
            { "inner" : { "displayedString" : "50 AOI one"},
            { "inner" : { "displayedString" : "The ABI one"}
            { "inner" : { "displayedString" : "AOI one"},
            { "inner" : { "displayedString" : "BOI one"},
            ]
}

The sorting logic has to be 0-9 < A-Z i.e, numbers take priority over the alphabets and also if the displayedString contains "The" as the first word, I need to remove/ignore it and continue with my sorting logic.
For example, in the above "The 40 AOI one" is prior to "50 AOI one" since "The" is removed and 4<5. similarly with "The ABI one" and "AOI one".
How can I verify the json input follows the logic provided?

2

Answers


  1. Sort a copy of your array and compare with original.

    While sorting remove The prefix with /^thes+/i regular expression and adjust the collator options to your needs.

    const original = data.outer.map(({inner:{displayedString: str}}) => str);
    
    console.log(JSON.stringify(original));
    
    const compare = new Intl.Collator('en', {numeric:true}).compare;
    const re = /^thes+/i; // removes the first The
    
    const valid = original.slice()
      .sort((a, b) => compare(a.replace(re,''),b.replace(re,'')))
      .every((item, idx) => item === original[idx]);
      
    
    console.log('VALID SORTING:', valid);
    .as-console-wrapper { top: 0; max-height: 100% !important; }
    <script>
    const data = {  
      "outer": [ 
                { "inner" : { "displayedString" : "The 40 AOI one"}},
                { "inner" : { "displayedString" : "50 AOI one"}},
                { "inner" : { "displayedString" : "The ABI one"}},
                { "inner" : { "displayedString" : "AOI one"}},
                { "inner" : { "displayedString" : "BOI one"}},
                ]
    };
    </script>
    Login or Signup to reply.
  2. Create a copy of your list, define a custom sorting function to sort the list of strings based on the given criteria. Finally, you can compare the original list with the sorted list to check if they match.

        const json_data = `{
      "outer": [
        { "inner": { "displayedString": "The 40 AOI one" } },
        { "inner": { "displayedString": "50 AOI one" } },
        { "inner": { "displayedString": "The ABI one" } },
        { "inner": { "displayedString": "AOI one" } },
        { "inner": { "displayedString": "BOI one" } }
      ]
    }`;
    
    // Parse the JSON data
    const data = JSON.parse(json_data);
    
    // Extract the displayedString values
    const displayedStrings = data.outer.map(item => item.inner.displayedString);
    
    // Define a custom sorting function
    function customSort(a, b) {
      // Helper function to remove "The " and convert to lowercase
      const normalizeString = str => str.toLowerCase().replace(/^the /, '');
    
      // Apply sorting logic
      const normalizedA = normalizeString(a);
      const normalizedB = normalizeString(b);
    
      if (normalizedA < normalizedB) return -1;
      if (normalizedA > normalizedB) return 1;
      return 0;
    }
    
    // Sort the displayed strings using the custom sorting function
    const sortedStrings = displayedStrings.slice().sort(customSort);
    
    // Check if the original list matches the sorted list
    const isSorted = JSON.stringify(displayedStrings) === JSON.stringify(sortedStrings);
    
    if (isSorted) {
      console.log("The JSON data follows the specified sorting logic.");
    } else {
      console.log("The JSON data does not follow the specified sorting logic.");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search