Sorting an array according to Priotity value
#1

Hello.
I've been trying to implement array sorting but so far I can't wrap my head around it.


PHP код:
#define MAX_ITEMS 20
new Items[MAX_ITEMS]; 
Somewhere in the code there is a function which returns Priority according to value of the array:
PHP код:
ItemPriority(ItemID)
{
    new 
Priority;
    switch(
ItemID)
    {
        case 
1Priority 1;
        case 
2Priority 2;
        case 
3Priority 2;
        case 
4Priority 2;
        case 
5Priority 1;
        case 
6Priority 2;
        case 
7Priority 3;
        case 
8Priority 2;
    }
    return 
Priority;

And now, how can I sort an array so first there are items with Priority 1, then 2, etc?

Example array before sorting:
PHP код:
Items[0] = 3// Priority = 2
Items[1] = 7// Priority = 3
Items[2] = 1// Priority = 1
Items[3] = 5// Priority = 1 
and after:
PHP код:
Items[0] = 1// Priority = 1
Items[1] = 5// Priority = 1
Items[2] = 3// Priority = 2
Items[3] = 7// Priority = 3 
Anyone?
Reply
#2

EDIT: Realized this wouldn't fix anything related to the problem, sorry
Reply
#3

bump
Reply
#4

A little insertion sort for small arrays should do the job
PHP код:
stock InsertionSortItem(array[], size sizeof array) {
    new
        
i,
        
j,
        
value,
        
priority
    
;
    while(
size) {
        
priority =  ItemPriority((value = array[i]));
        for(
1>= && ItemPriority(array[j]) > priority; --j) {
            array[
1] = array[j];
        }
        array[
1] = value;
        
i++;
    }

You should add a default value of invalid items
PHP код:
ItemPriority(ItemID)
{
    new 
Priority cellmax;
//... 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)