Sorting Array values from LARGEST to SMALLEST

  • Hi!

    It seems that the sort action of an array just sorts alphabetically?? (see photo) 16 > 2?

    Can anyone provide me how will i achieve to sort my values from LARGEST to SMALLEST?

    Why is construct does not give users option on sorting..

    Thank you!

  • Sort it ascending and then reverse it.

    Check this example to see how.

    Hope this helps.

    Cheers!

  • But be careful, if you have Steph Curry and Kawhi Leonard on your list they will always come out on top, no matter how you sort it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • But be careful, if you have Steph Curry and Kawhi Leonard on your list they will always come out on top, no matter how you sort it.

    Hahahahahahahaha

  • brunopalermo

    Thank you for the example but it seems that yours only works on "start of layout" ? Im trying to sort the array once AJAX has completed getting data and storing to my array, but when i applied to mine, it just ends up only being reversed

    And yes, i have tried to change your "repeat 30 times" to my "For each X element" and it worked fine like yours did

    Your sample is what im exactly looking for and im hoping you can help me further on this. Thank you!

    BadMario Ha! I understood that reference

  • You'd just have to put it under "On AJAX_EVENT_NAME complete", and call the AJAX event "on start of layout". It should work no matter where you put this.

    If you can share your code, I'd gladly fix it.

    Cheers!

  • brunopalermo

    https://drive.google.com/uc?export=download&id=1JwdQaRy__jCvAde8LrnIqGSRdhtyWHGg

    Thank you so much for trying to help! :)

    EDIT: please try to copy the WHOLE link and paste to download, im getting an error when just CLICKING it. apologies

  • There are two big problems in your file.

    1. When you get the stats and fill the array, the numbers are filled as STRINGS, probably because they come from a CSV. When you fill up the array 03_Live _Stats you should use int() in the Value field:

    int(02_Todays_stats.At(03_Live_stats.CurX,1))
    

    Also, you dont need "VAriable3", just use "03_Live_stats.CurX" as I did above.

    2. You're only getting the first 10 players from your 02_Today_Stats, so, you're not listing the 10 best. You're just showing the first ten ordered by total score. 03_Live_Stats should have the same length as 02_Today_Stats. So, when you transfer players all of them will be there. Thus, when you sort 03_Live_Stats and reverse, you'll get them all. After that you can choose to only show the first 10, which will be the best ones.

    EDIT: Check this file for the solution.

    Hope this helps...

    Cheers!

  • brunopalermo

    Ill check this tomorrow, its 12 am here :D ill be back at the same time so we can discuss more(maybe?). I really really appreciate this, thank you !

  • I got it now! brunopalermo

    I understand now, of course im "copying" the data of 02_todays_stats so i need to loop very curX of 02_today array (as you said!), and then store to 03_live_stats.

    I studied well your codes and converted it to what im more familiar with. It works fine and result are the same.

    That "int" (what do u call that? expression?) is a new thing for me and i thank you.

  • Yep. int(x) is a system expression to convert a float or a text to a integer. There are also str(x), which is almost never needed but may be and converts float or integer to a string, and float(x) that converts an integer or text to a float, that is, a fractional number like 5.2.

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