How do I sort an array by writing

0 favourites
From the Asset Store
A cool way for kids to write and practice English Alphabets
  • Hi all, I'm going crazy with arrays again... :D

    I have an array that I insert and remove words into during the game and that I sort alphabetically each time

    apple

    Banana

    blueberries

    pear

    potatoes

    raspberries

    strawberries

    Is there a way to sort the array based on the letters I type in a text field? For example if I type "g", it selects the words pear and potatoes, if I type "po", it will find only potatoes

  • In general what you are looking to do is compare text with an expression - find(array value, text input) will look to see if the text input value exists in the array text.

    You would use system compare two values - find(array value, text input) is the first value and then you would say 'not equal to' and -1 is the second value, this now returns true if your text entry is contained in the array value. You would replace array value with array.at(loopindex) when it is put inside a loop to run through the rows of the array.

    On the action side of the loop to filter the elements it depends how you are using the array and selecting its elements in the game. You could delete the row if it doesn't match, this would mean changing the condition above to 'equal to -1', then you would be left with only matching values.

  • I did some tests before writing here, but I have no idea what I'm doing. I couldn't find examples

    Not knowing how to handle loops and replace values, I tried to start with something simpler, like deleting a row that corresponds to the written word, but I still couldn't...

    sendspace.com/file/6pw13x

  • Check out this demo

    https://howtoconstructdemos.com/search-for-a-text-in-a-large-list-by-typing-first-letters-capx/

    This is really helpful thanks!! I merged that with my example, it seems to work, do you know if there's a reason why with my system when I type the P, it finds me Potatoes instead of Pear? It's like reversing the alphabetical order.

    sendspace.com/file/2ku48h

  • You shouldn't set those text inputs inside the loop or inside the function. To be honest it's not clear what they are for. If they are there to show the first and second value of the array then its array.at(0) and array.at(1).

  • I was hoping that by typing "P" Pear would appear in the first text field and Potatoes in the second, I'm making attempts.

    But I don't understand why even if I leave only the first text, when I write P it finds Potatoes first thus reversing the alphabetical order while it doesn't happen in the list

  • This is why I rarely share files because you end up with a bunch of events you don't really understand, for me that's not the way to learn Construct. It is not reversing any order you just put the action in a loop so it will run until the last element is found. If you want to show elements 0 and 1 from the array then do as I mentioned above outside of any loop or function.

  • Maybe I explained myself badly, I don't want the first 2 rows of the array, fortunately I know how to show them :D

    I would like that by typing the letter P the first text finds me Pear and the second Potatoes.

    instead now when I type P it finds me only potato, while in the list it finds Pear

  • Maybe I explained myself badly, I don't want the first 2 rows of the array, fortunately I know how to show them :D

    I would like that by typing the letter P the first text finds me Pear and the second Potatoes.

    instead now when I type P it finds me only potato, while in the list it finds Pear

    Find? Meaning what? Yes it's badly explained.

  • Given our nicknames, I apologize from Leon to Leone but I use google translate to write on the forum

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So what are you trying to do in the textboxes?

  • So what are you trying to do in the textboxes?

    This code should be clearer

    sendspace.com/file/3550lc

    The first thing I would like to understand is why when writing the letter "p" in the input text there is the word potatoes and not pear, I wish there was pear because putting the words in alphabetical order it comes first

  • Cannot reproduce that on the file sent

  • What can't you reproduce? Do I have to resend the file?

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