Arrays - Sort By X

3 favourites
  • 6 posts
  • Thought it'd be a good suggestion to have an action in Arrays to where sorting can be done by column of the array. So instead of just sorting the whole array by either axis, you'd be sorting by, say, column 3 (x = 2), so that if you have a database of items, you could choose which column you wanted to sort by for inventory purposes.


  • I do a lot of database work with C3.

    What I do is make the array a container and add two additional arrays - one called Filter, and one called Index. And I also add a dictionary so I can use field names instead of numbers for the columns.

    The Filter array only has one column. If I want to show a subset of the records in the main array, Filter only contains the row number of the main array for records that meet my criteria.

    Then to sort the array, I use the Index array. It has two columns - the first column contains the field (column from the main array) I want to sort by, the second column contains the row number from the main array.

    To build the index, I loop through the Filter array so only records that match the current filter are included. For each row I copy the field I want to index by to the index array (and its row number). Sort the index array by the x-axis (and reverse if I want it in descending order).

    I have used this with arrays that have thousands of records, and it works great - even on mobile! I can filter and sort a very large array instantly.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for that thorough explanation! I can definitely work with this. It's not an ideal solution, but hopefully anything that comes out of challenges with a software can be used to help the software evolve! Maybe they'll fix this in the NEAR future to be easier to manipulate??? Would you say that, if they did have the 'sort by x' feature (and that filter function as well) that it would be the right solution for this challenge? Or do you feel that your solution is easy to set up from scratch every time?

  • certainly would be easier to be able to sort the main array by any column you want, but it isn't too hard to do it the way I do. Plus I can control exactly how it works - I can combine multiple columns for a primary and secondary sort order, control whether it sorts alphabetically or numerically. I have functions to do all the work. I set instance variables on the main array that indicate what column to sort by, numeric or alpha, ascending, descending, etc... I pass the UID of the array into the function because I usually have arrays with data from different tables from a database. Loading the arrays with AJAX calls also loads the field names into the dictionary, so I can change the query and don't have to worry about changing any code since I don't access data by column number, only by field name.

  • AllanR - If you could be so kind, the more I read this the more confused I get. Mainly because the way that Arrays seem to work is very 45-degree-axis-flipped from the way they normally work, at least in the inspector. In my understanding, a row is a record, but it seems that in Construct 3 arrays, a row is a field type (what would otherwise be a column in, say, a Google/Excel spreadsheet). But this makes it insanely difficult (maybe just for me to twist my mind to make it make sense) to READ in the Inspector at runtime. I want to see it like it would be a spreadsheet, but it shows in ROWS of X index????

    How is one supposed to learn this stuff if it's mirror-flipped twice?

  • it does get a little confusing at first...

    I don't think of it in x,y coordinates - I always think of it as row,column

    row is the array width, column is the array height

    here is a very stripped down sample of my array functions for loading, filtering, sorting and displaying array data:

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)