Thread: New Sort an Array of Classes

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    New Sort an Array of Classes

    I am new to C++. I am look for the best way to sort a structure or class array. (I hope I am using the right terms

    Problem
    I have a || array of 5 items with name=(john, jim, danny, spencer, greg)

    They have tuition = (10., 12, 11, 14, 18)

    I would like to change this to a class and be able to sort on either tuition or names.

    What is the best way to do this in c++

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The best way in C++ would be to define two sorting predicates for your class. One that sorts based on the name and one that sorts based on the tuition. This is just a function that returns a bool and takes two (const) references to your class. It should return true if the frst argument is less than the second, and false if they are equal or if the second argument is less than the first. These function should be stand-alone functions, not part of your class.

    Then, you can sort your array (or vector which is preferred over arrays in C++) using std::sort and passing in the sorting predicate you want to use - either the one on the name or the one on the tuition.

    You should probably be using the C++ string class to hold your names. If you are not, then this solution might be more complicated since you would have to add copy-ability to your class.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    At Cprogramming.com's sister site, AIHorizon.com, you'll find several articles that teach you various sort algorithms, and show you how to implement them in C++.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Bottom Posted reply

    Quote Originally Posted by Daved
    The best way in C++ would be to define two sorting predicates for your class. One that sorts based on the name and one that sorts based on the tuition. This is just a function that returns a bool and takes two (const) references to your class. It should return true if the frst argument is less than the second, and false if they are equal or if the second argument is less than the first. These function should be stand-alone functions, not part of your class.

    Then, you can sort your array (or vector which is preferred over arrays in C++) using std::sort and passing in the sorting predicate you want to use - either the one on the name or the one on the tuition.

    You should probably be using the C++ string class to hold your names. If you are not, then this solution might be more complicated since you would have to add copy-ability to your class.

    Does the standard std::sort sort the whole class? simple case listed
    Code:
    Class School
    {
    public
       string name;
       float tuition;
    };
    
    int main()
    {
        School mySchool [5] = {School ('John", 10.),
                                              School ('Jim", 11.),
                                              School ('Greg", 12.),
                                               School ('Spencer", 13.),
                                              School ('Joan", 14.)};
    /*  enter sort syntax here  Can I use std::sort and how?*/
        myFunctionSortSyntax ("Sort my mySchool.name");
        myPrintSortedLists ("Print the Sorted with with name and tuition");
    
    return EXIT_SUCCESS;
    }

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    You can probably, make your own program which sorts numbers into order. However, sorting names would be slightly more tricky.


    In that case this thread may be of interest to you

    http://cboard.cprogramming.com/showt...t+alphabetical

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    I can do a sort, but need to sort a array of classes

    Thanks for the help, but what I need is an example of how to sort a class array or structure array.

    The only way I seem to get it to work is by using || arrays. Treating both name and tuition as seperate arrays, and doing multiple swaps. I cannot see a way to use vectors. This can be very cumbersome for a large class or structure with many items.

    Is there one exaample out there of sorting a array sturcture? All I seem to find is single items.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Did you even click on the link I gave you? You look under "Basic CS" and there're 5 sort algorithms, with source code, and they all use arrays. Just treat your classes like single items.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    Yes, in fact I have compiled and run a couple.

    What do I replace
    vector<string> names;
    with when I I use the school class from above?

    what do I put for names below
    while (inName >> name)
    names.push_back(name);

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To use std::sort with an array, you pass the beginning of the array and one past the end of the array to the function. Because of the properties of arrays, it as simple as using mySchool and mySchool + 5. Since your array holds your own class, you need to define a sort for that class. Here is the shell of one sorting function, you need to fill in the code and do one for tuition.
    Code:
    bool SortSchoolByName(const School& left, const School& right)
    {
       // return true if left is less than right.
       // otherwise return false
       //   (they are equal or right is less than left).
    }
    
       // call the sort like this (assuming array has size of 5):
       std::sort(mySchool, mySchool + 5, SortSchoolByName);
       // Or use a vector instead of the array and sort like this:
       std::sort(mySchoolVec.begin(), mySchoolVec.end(), SortSchoolByName);

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    11

    I think I need to start new tread.

    I believe I do not understand the releationship of vectors and class arrays. I will start a new tread so I can start get back to this one when I have a couple basics

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Vectors are essintialy arrays that grow. The term class array is used for user defined types in an array. Arrays have no bounds checking and if your not careful you may have buffer overuns which are security leaks.

    I hope that helps.

    One other note vectors are essentialy templates while arrays are like primitave data types.

    You senior members can fill in what I am ignorant of.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    A vector is basically an array with tonnes of built in features. It can grow as you need it, and has a lot of related functions to make it easier to work with.

    You can make an array of classes that you have define, and do the same thing to that array that the other code does to Vectors. Don't just copy the code - look at what the code does to solve the problem, and then apply that same method to your situation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort array seg fault
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 12-24-2008, 01:28 PM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 08:27 PM