Raised This Month: $ Target: $400
 0% 

Sorting an array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-15-2012 , 16:44   Sorting an array
Reply With Quote #1

hoekay, i have an array that i want to sort from highest to lowest. And in an attempt to do so i ran into 2 questions.

1: why can't i declare an array as such:
Spoiler


2: is this right, and efficient? The wiki was kinda vague.
Spoiler
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-15-2012 , 17:25   Re: Sorting an array
Reply With Quote #2

1. Array sizes have to be constant and cannot be dependent on a variable.
If you need to have a dynamic array size, look into cell arrays.

2. You don't even need that list.
Code:
public cmdRank(id)  // Possibly replace nVault with SQL, makes sorting easier and more efficient {     if(get_pcvar_num(toggle_pcvar) == 0 || get_pcvar_num(frags_pcvar) )         return PLUGIN_CONTINUE             // devlop to MOTD listing of server-wide rank (connected players     new players[32], num     get_players(players, num, "ch")         SortCustom1D(players, num, "sortHelper")         // players are sorted         return PLUGIN_HANDLED } public sortHelper(const index1, const index2, const players[], const data[], const data_size) {     new diff = gi_playerFrags[players[index2]] - gi_playerFrags[players[index1]]         return (diff < 0) ? -1 : (diff ? 1 : 0) }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-15-2012 , 20:47   Re: Sorting an array
Reply With Quote #3

Quote:
Originally Posted by Exolent[jNr] View Post
1. Array sizes have to be constant and cannot be dependent on a variable.
If you need to have a dynamic array size, look into cell arrays.

2. You don't even need that list.
Code:
public cmdRank(id)  // Possibly replace nVault with SQL, makes sorting easier and more efficient {     if(get_pcvar_num(toggle_pcvar) == 0 || get_pcvar_num(frags_pcvar) )         return PLUGIN_CONTINUE             // devlop to MOTD listing of server-wide rank (connected players     new players[32], num     get_players(players, num, "ch")         SortCustom1D(players, num, "sortHelper")         // players are sorted         return PLUGIN_HANDLED } public sortHelper(const index1, const index2, const players[], const data[], const data_size) {     new diff = gi_playerFrags[players[index2]] - gi_playerFrags[players[index1]]         return (diff < 0) ? -1 : (diff ? 1 : 0) }
1. I only wanted it dynamic like that so i wouldn't have to have more cells then needed. But if its not possible, screw it.

2. wouldn't that change the frags for the players? because frags are stored in the cell index that correlates to their playerID, this would rearrange values, but not IDs. I need to keep both numbers together.

Though i must say....that return statement gives me a nerd-on.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-15-2012 , 21:04   Re: Sorting an array
Reply With Quote #4

It doesn't even change the player's frags or even how they are associated.
It just reorders the players array.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-16-2012 , 01:24   Re: Sorting an array
Reply With Quote #5

Quote:
Originally Posted by Exolent[jNr] View Post
It doesn't even change the player's frags or even how they are associated.
It just reorders the players array.
OH! durr. I feel dumb. Its a trend this week....
Ordering the players array by frags is a great idea! Thanks, man. What would i do without you? Other than have horribly inefficient code.....

Also....does anyone know what kind of sort algorithm they use for these? I'd assume a bubble sort, which would make me want to get friskey with new algorithms, if i ever care enough to do so.

EDIT: They use Quicksort. (should have guessed-but i forgot about) which i suppose is the best way to do it in a lower-level language such as pawn. And if i ever figure out an easy way to do objects....i just might write a tree-sort. But objects will be relatively difficult. And even then....i'm sure it'll be way too memory intensive for the people around here....even if it is O(log n)

EDIT AGAIN: I figured out a way to do it. structure{[value], [leftIndex], [rightIndex]} done in array format We'll see where this takes us when i get around to coding it....
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-16-2012 at 12:55.
Liverwiz is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:06.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode