How to sort an array in JSON

Not favoritedFavorited Favorited 0 favourites
  • 3 posts
From the Asset Store
A master sorting Puzzle fun and addictive puzzle game! A challenging yet relaxing game to exercise your brain!
  • There's no direct way to sort an array in JSON within Construct 3. However, there's a way to do it using an intermediate Array object. I want to show how this can be done, in case it's helpful to others.

    So, imagine you're using JSON for a leaderboard. Inside your JSON, you have a "leaderboard" object containing an array with player data (username, score, avatar, etc.).

    Here are the events:

    Here is the result:

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I've just built a "level factory" to build levels that are stored as an array of objects in JSON. I had to use JSON because two of the level parameters are themselves arrays. I can then assign a difficulty that I want to sort the levels by. But I need to download the JSON so I can re-import it as a project file, so currently plan to sort it using an online tool. But I will bookmark this in case I need to sort it at runtime.

  • C3 for vanilla JSON ACE is poor, so I'll opt to use Javascript to help me with this.

    Note that to keep the function simple, the parameter passed in here is the array containing those items, not the outer JSON object.

    JSON.GetAsCompactString("leaderboard")
    

    Export asJSON to the Function, Convert the jsonString to json object and process it via Javascript. Finally, convert the json object back to a string and return it.

    // [...]
    const arrayData = JSON.parse(localVars.jsonString);
    
    // Sort Function
    const sortArray = (arr, key, asc = true) =>
     [...arr].sort((a, b) => asc ? a[key] - b[key] : b[key] - a[key]);
    
    // Sort
    const sortedData = sortArray(arrayData, 'score', false);
    
    // return String
    runtime.setReturnValue(JSON.stringify(sortedData));
    

    Function Clipboard. Need 'JSON' Object

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"parse","objectClass":"JSON","parameters":{"data":"Functions.sortArray(JSON.GetAsCompactString(\"leaderboard\"))"}}]},{"functionName":"sortArray","functionDescription":"","functionCategory":"","functionReturnType":"string","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"jsonString","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[{"type":"script","language":"javascript","script":["// [...]","const arrayData = JSON.parse(localVars.jsonString);","","// Sort Function","const sortArray = (arr, key, asc = true) =>"," [...arr].sort((a, b) => asc ? a[key] - b[key] : b[key] - a[key]);","","// Sort","const sortedData = sortArray(arrayData, 'score', false);","","// return String","runtime.setReturnValue(JSON.stringify(sortedData));"]}]}]}
    
Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)