How do I sort text array values?

Get help using Construct 2

Post » Mon Jan 23, 2017 2:05 am

Hey guys,

I'm trying to learn how to sort my array values that contains a specific text
I just want to be able to pick a text, and move them to the top of my list.
I don't want ascending/descending orders of numbers, I just want to isolate a category if that makes sense! :D

Example:
1,5,Grape
10,4,Grape
3,6,Grape
5,7,Apple
3,5,Pineapple
B
17
S
6
G
1
Posts: 79
Reputation: 2,422

Post » Mon Jan 23, 2017 10:55 am

I could be wrong - but I'm pretty sure the in-built sort feature includes alphabetical as well as numerical searching. You'd have to move the text over to be the first index on the X axis, but then the in-built sort function should work fine.

EDIT: Sorry - just re-read your post. To move a group to the top of the list, simple iterate through the array checking for the identifier you're looking for. When you find that identifier, push a new value to the front of the array containing the values from that index - then delete the original index.

If you want everything grouped beforehand, you'll need a place to store a list of all of the text values stored in the third column, then perform the above process for each item on that list.
The LEVEL GENERATOR TOOLKIT



24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 242
Reputation: 2,306

Post » Mon Jan 23, 2017 5:56 pm

If you set the text as the first index then you could just use the sort action.

As is the first value is a number so it would be sorted like this:
1,5,Grape
3,6,Grape
3,5,Pineapple
5,7,Apple
10,4,Grape

but if you set the array up like this:
grape,1,5
grape,10,4
grape,3,6
apple,5,7
pineapple,3,5

then sort would do make it like this:
apple,5,7
grape,1,5
grape,10,4
grape,3,6
pineapple,3,5

Besides that you could implement your own sorting. "bubble sort" is probably the simplest to do just look at wikipedia for some psuedo code. But it's probably easier to just rearrange your data and use the sorting action.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Tue Jan 24, 2017 4:00 am

I ended up going with R0J0hound's method, thank you so much! that worked for me =)
yeah, its weird...i cant seem to sort from the 3rd value =/ does it only work on the first?
B
17
S
6
G
1
Posts: 79
Reputation: 2,422

Post » Tue Jan 24, 2017 5:06 am

Yeah, only the first value is used for sorting.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991


Return to How do I....?

Who is online

Users browsing this forum: adventurist and 39 guests