Archived

This topic is now archived and is closed to further replies.

amemorex

sorting an array of classes?

Recommended Posts

if i have an array similar to this:
  
class TEST {
   char name[32];
}; 

void main() {
  TEST list[8];
}
  
if i want to sort that array alphabetically (based on the name inside it), would simply changing like [0] to [1] and [1] to [0] in the array work, or do i have to individually recopy every member of the class to the other element of the array.. also, i am really unsure how to sort a class, i know how to make a swap function for variables, but i don''t know how to swap 2 elements of an array of classes...any help?

Share this post


Link to post
Share on other sites
The only way you can sort an abstract data type on its own is by its memory address, and that''s not all that useful. You can however sort it by member data like the char array in your example. You could make a generic sort function that implements a sorting algorithm like quicksort and go to town on your array. The easiest way might be to create an empty array and to copy the data into it as it gets sorted. I haven''t done much sorting work myself so there are probably better ways to do it, and I''m sure I''ll be corrected on this. ; ) Why don''t you go to google an look it up anyway? There are so many resources on sorting out there its not even funny. Just give it a try.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Depends on the class whether or not you can just use [0] = [1] and [1] = [0]. If the members are all simple C++ data types like int, char, etc. then it will work. Otherwise you must implement a copy constructor or you will get a shallow copy.

Share this post


Link to post
Share on other sites
  
HASH_include <algorithm> // note: HASH_ means #

HASH_include <functional>
using namespace std;
struct PredCompTest : binary_function<const TEST&, const TEST&, bool>
{
bool operator () (const Test &lhs, const Test &rhs) const
{
const int lhs_len = strlen (lhs.name);
const int rhs_len = strlen (rhs.name);
return lexicographical_compare (
&lhs.name[0], &lhs.name[lhs.len],
&rhs.name[0], &rhs.name[rhs.len);
}
};

int main ()
{
TEST list[8
];
initialize_list (list);
sort (&list[0], &list[8], PredCompTest ());
assert (is_sorted (list));
return 0;
}

(note, didn''t compile it so I apologize in advance for any errors).
This is a general approach. However, there is the efficiency problem that the string length must be computed for each argument for each comparison. To alleviate this, you could use std::string instead of a fixed-length array.
You could also overload operator < for TEST instead of creating an explicit predicate and use the 2-argument version of sort, but this would suggest that this is the _only_ way in which your list may be sorted.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
easy, just use the &lt (less-than) symbol. It is an operator which means it is a function that you can define.
  
bool operator< (const Test t)
{
//put code here, such as doing some sort

//of compare using those arrays, or just

//use the standard string class like you should

if(/*blah blah*/)
return true;
else
return false;
}

//ok now you have your operator< written, time to sort:

#include <algorithim>
using namespace std;

//make your array (you should use a vector actually)

//put values in it


sort(list,list+length);//if you are using a silly array

sort(list.begin(),list.end());//if you are using a cool vector


//see how nice the sort function is? It can sort anything!




Share this post


Link to post
Share on other sites