Jump to content
Crazy_Man

Sort an array of array only by number

Recommended Posts

Hello, I want to sort an array of array with strings and numbers but only on the number. With the highest number at the start of the array.

_array = [["Player 1",1], ["Player 2",3], ["Player 3", 2]];

// The result I want is :

_array = [["Player 2",3], ["Player 3",2], ["Player 1", 1]];

 

Share this post


Link to post
Share on other sites

Re-arrange your original array so that the number goes first. Is there a specific reason you want string be the 1st element? 

 

  • Thanks 1

Share this post


Link to post
Share on other sites
6 hours ago, Crazy_Man said:

Hello, I want to sort an array of array with strings and numbers but only on the number. With the highest number at the start of the array.


_array = [["Player 1",1], ["Player 2",3], ["Player 3", 2]];

// The result I want is :

_array = [["Player 2",3], ["Player 3",2], ["Player 1", 1]];

 

 

Simple sorting without having to manually do anything:

_array = [["Player_1",1],["Player_2",5],["Player_3",2]];
_toSort = _array apply {[_x#1,_x#0]};//number is now first element
_toSort sort false;//false for descending order
_array = _toSort apply {[_x#1,_x#0]};//switch it again back to original order ["string",number]
systemchat str _array;
//prints:
//[["Player_2",5],["Player_3",2],["Player_1",1]]

Cheers

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×