CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Ordering

  1. #1
    Join Date
    Feb 2004
    Posts
    232

    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!

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is an STL container such as a set.

    Kuphryn

  3. #3
    Join Date
    Jun 2004
    Posts
    106
    I would have have an array of structures and then use qsort()

    Yasoo
    Z++ Technologies

  4. #4
    Join Date
    Feb 2004
    Posts
    232
    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!

  5. #5
    Join Date
    Jun 2004
    Posts
    106
    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    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

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    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

  8. #8
    Join Date
    Feb 2004
    Posts
    232
    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!

  9. #9
    Join Date
    Feb 2004
    Posts
    232
    *bump bump bump*
    Need help with anything related to audio programming? I can help!

  10. #10
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Quote 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?

  11. #11
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    How about using a priority queue.

  12. #12
    Join Date
    Feb 2004
    Posts
    232
    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!

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    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.

  14. #14
    Join Date
    Feb 2004
    Posts
    232
    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!

  15. #15
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    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
  •  





Click Here to Expand Forum to Full Width

Featured