Sorting structures

This is a discussion on Sorting structures within the C++ Programming forums, part of the General Programming Boards category; Next problem :-) I have created a class to do several things. It defines a structure that contains 4 columns. ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Sorting structures

    Next problem :-)

    I have created a class to do several things. It defines a structure that contains 4 columns. I have a list of these structures contained in an array. What i am wanting to do is allow the user to define with column they would like to sort by but I cant quite figure out what I need to do. I figured I could write a functions for each column, but am unclear on how to implement it. I can provide my entire code, or snippets if necessary; else just point me in the right direction please!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why not use a std::vector instead of an array?
    You can just make four functions. Each for every column. Then the value you use to sort will be that column, but when you swap, you can swap the structure itself. For example, you have two structures lets say in a sorting function:
    Code:
    void sortCol1(myStruct[] structs)
    {
       ...
       if (myStruct[0]->column1 > myStruct[1]->column1)
       {
           //swap structures
           myStruct* temp = myStruct[0];
           myStruct[1] = myStruct[0];
           myStruct[0] = temp;
       }
    }
    You can combine then the 4 functions into one and add the column as a parameters.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Well, Im using arrays because the assignment requires. :-(

    That explanation does help though. Only thing im confused about is how to call the columns.
    My Declaration of class and structure looks like this
    Code:
    class Format{
    	char dlimit;
    	int count;
    	ifstream infile;
    	ofstream outfile;
    	struct {
    		string c1,c2,c3,c4;
    	} title;
    	struct {
    		string c1,c2,c3,c4;
    	} aline;
    	struct {
    		string name;
    		float float1;
    		float float2;
    		int int1;
    	} list[100];

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > string c1,c2,c3,c4;
    Well saying
    string columns[4];
    would make it a lot easier to say (using a number) which column you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    ahhh, thanks :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  3. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 02:02 PM
  4. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21