-
July 11th, 2004, 06:13 PM
#1
Ordering
I have a project that I'm working on, and it concerns a mass userbase. I ave a structure containing information about each user. There is one variable, priority, which is self explainatory. What I want to do is organize each one by higher priority. Like the dude with highest priority is [0], then 2nd highest [1]. How can I do this?
Need help with anything related to audio programming? I can help!
-
July 11th, 2004, 10:01 PM
#2
One solution is an STL container such as a set.
Kuphryn
-
July 11th, 2004, 10:23 PM
#3
I would have have an array of structures and then use qsort()
Yasoo
Z++ Technologies
-
July 12th, 2004, 03:30 AM
#4
But the number is inside the structure.
Code:
typedef struct user_tag
{
int priority;
} user;
user users[2000];
Need help with anything related to audio programming? I can help!
-
July 12th, 2004, 01:17 PM
#5
Check out the docs for qsort. One of the arguments is:
int (__cdecl *compare )(const void *elem1, const void *elem2 )
This is the function that you create which qsort calls to do the comparison. In that function you would do something like:
Code:
if ( ((struct user_tag *)elem1)->priority > ((struct user_tag *)elem2)->priority )
return 1;
else if ( ((struct user_tag *)elem1)->priority < ((struct user_tag *)elem2)->priority )
return -1;
else
return 0;
Yasoo
Z++ Technologies
-
July 12th, 2004, 01:25 PM
#6
Originally posted by Ness
But the number is inside the structure.
Code:
typedef struct user_tag
{
int priority;
} user;
user users[2000];
Is this C or C++? If it's C++, there is std::sort. Also, the typedef is not needed for C++.
Code:
#include <algortihm>
struct user
{
int priority;
};
bool SortByPriority( const user& first, const user& second)
{
return first.priority < second.priority;
}
int main()
{
user users[1000];
//...
std::sort( users, users + 1000, SortByPriority );
}
Qsort may work, but using std::sort is the preferred way to do this for C++ programs (note that there are no casts necessary whatsoever for std::sort).
Regards,
Paul McKenzie
-
July 12th, 2004, 01:26 PM
#7
Originally posted by Yasoo
I would have have an array of structures and then use qsort()
Yasoo
Z++ Technologies
I agree with the array of structures, but if it is a C++ program, usage of std::sort is preferred over qsort for many reasons.
Regards,
Paul McKenzie
-
July 13th, 2004, 10:10 PM
#8
When I use std::sort, it just clears my whole array, leaving absolutly nothing in it. What's wrong?
Need help with anything related to audio programming? I can help!
-
July 14th, 2004, 01:26 AM
#9
Need help with anything related to audio programming? I can help!
-
July 14th, 2004, 01:36 AM
#10
Originally Posted by Ness
When I use std::sort, it just clears my whole array, leaving absolutly nothing in it. What's wrong?
How can we know without seeing your code using std::sort?
-
July 14th, 2004, 02:44 AM
#11
How about using a priority queue.
-
July 14th, 2004, 12:33 PM
#12
This is my complete structure (replace priority with coins):
Code:
typedef struct user_tag
{
char username[13];
char password[13];
char NAME[13];
char ip[17];
char avatar[500];
char location[25];
int usernumber;
int wins;
int lost;
int games;
int percentofwin;
int coins;
bool login;
int acceptNumber;
int track[16];
} user;
user users[2000]
And here's the complete code:
Code:
bool SortByPriority( const user& first, const user& second)
{
/*if(first.coins == second.coins)
{
return first.usernumber < second.usernumber;
}
else
{
return first.coins < second.coins;
}*/
return first.coins < second.coins;
}
// another function...
std::sort( users, users + 2000, SortByPriority );
I fill the structures before I sort them. What's wrong now?
Need help with anything related to audio programming? I can help!
-
July 14th, 2004, 12:39 PM
#13
Code:
std::sort( users, users + 2000, SortByPriority );
Are there actually 2000 elements in the array ? I know 2000
is the dimension of the array, but how much of the array do
you actually use ? The "2000" in the code above should be
the actual number of elements that have been given values.
-
July 14th, 2004, 01:21 PM
#14
Thanks a bunch, but theres ANOTHER problem. How do I make it sort grom greatest to least?
Need help with anything related to audio programming? I can help!
-
July 14th, 2004, 01:25 PM
#15
The SortByPriority() function determines how the elements
are sorted. You can change it to sort any way you want :
Code:
bool SortByPriority( const user& first, const user& second)
{
return first.coins > second.coins; // largest to smallest
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|