Thread: sorting

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    sorting

    i was thinking of doing a sorting funciton using templates, but its not working out.
    What im trying to sort is one of the following:
    - animal type (string)
    - animal name (string)
    - animal id (int)
    - animal owner (string)

    i have a linked list of animals. where each animal has all this info.

    So if i wanted to print out a sorted list of animal names, i cant seem to do it with using templates.. or can i?
    i havnt begun coding, just incase if this is not a good way of doing it..

    for templates, i cant send it a variable saying i want to only sort animal id's or animal names.
    is there a way around this?
    thanks

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    yes you can use templates.
    make your sorting function and leave pointer to compare function
    as parameter to sorting function.
    -- Add Your Signature Here --

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    mmm not sure i understand you correctly.
    so i make my sorting template
    which takes an argument or a field (animal_name)
    and inside the sorting function i have the compare function?
    or.. ?

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    ok well im still confused, and i thought i would post an example, so that you might know more of what im trying to do..

    say i want to sort the animal_type.

    my sort function

    Code:
    sort(<type> a)
    {
     //sort this field
    }
    now is it possible to pass animal_type as a type?
    so that in the function sort, i know that i only need to sort the animal_type

    so can i do
    Code:
    sort(animal_type);
    sort(animal_id);
    keeping in mind that my animals are all stored in some sort of linked list.

  5. #5
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    now is it possible to pass animal_type as a type?
    so that in the function sort, i know that i only need to sort the animal_type
    instead of leaving animal to sorting function leave pointer to compare
    function or define compare function somewhere in your Animal class.
    then your list doesn't need to know what fields from class needs to be
    compared.

    Like:
    Code:
    sort ( ... )
    {
         if ( thisAnimal.compare( toThisAnimal ) ) {
              .... do something
         } 
    }
    //or leave compare function as an argument 
    sort ( cmprfunc_t animalcmpfunc, .... ) {
        ....
        if ( animalcmpfunc ( thisAnimal, thatAnimal ) ) {
            ... again do somthing to this list.
        }
        ....
    }
    but if you have only animals then you propably doesn't need templates.
    -- Add Your Signature Here --

  6. #6
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    sometimes leaving pointer to compare function is better solution because
    it can be decided on runtime what compare function is needed
    to use.

    if we have several columns in gui.
    and if user clicks one of these, we can call

    Code:
    myClickCallBack ( column column_number ) {
        ....
        sort ( list, cmpfuncs[column_number] );  // cmpfuncs[] = array of different comparison functions
        or. 
        list.sort ( cmpfuncs[column_number] );
        ....
    }
    Last edited by tjohnsson; 09-14-2004 at 04:04 AM. Reason: just killing my time...
    -- Add Your Signature Here --

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Is this a C++ STL list you are talking about or your own implementation of one? The STL list container has its own sort member function that accepts an argument to a comparison function you can define to handle various sort orders.

    Code:
    struct Animal
    {
        string type;
        string name;
        int id;
        string owner;
    };
    
    bool TypeSort(const& Animal lhs, const& Animal rhs)
    {
        return lhs.type < rhs.type;
    }
    
    bool NameSort(const& Animal lhs, const& Animal rhs)
    {
        return lhs.name < rhs.name;
    }
    
    bool IdSort(const& Animal lhs, const& Animal rhs)
    {
        return lhs.id < rhs.id;
    }
    
    bool OwnerSort(const& Animal lhs, const& Animal rhs)
    {
        return lhs.owner < rhs.owner;
    }
    
    ...
    
    list<Animal> Animals;
    
    //Sort Animals linked-list according to type
    Animals.sort(TypeSort);
    
    //Sort Animals linked-list according to name
    Animals.sort(NameSort);
    
    //Sort Animals linked-list according to id
    Animals.sort(IdSort);
    
    //Sort Animals linked-list according to owner
    Animals.sort(OwnerSort);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM