|
-
September 10th, 2002, 12:25 PM
#1
once more sorting structs....
hello,
i've implemented a structure like that:
struct file_struct
{
string strName;
string strType;
int nKB;
time_t date;
bool bIsDir;
};
these member-vars represent the attributes filled by the content of a directory, and the flag bIsDir shows if it is an directory or not.
the results are saved in an vector<file_struct>vc; for example:
1 ttt
2 adm(dir)
3 054(dir)
4 gah
5 j02(dir)
6 rec (dir)
7 fhr(dir)
now i've got 7 elements which are directories and 2 elements which are other files
i sort them like this:
int CompDir(const void* lhs, const void* rhs)
{
bool bLhs = ((file_struct*)lhs)->bIsDir;
bool bRhs = ((file_struct*)rhs)->bIsDir;
if (bLhs < bRhs) return 1;
if (bLhs > bRhs) return -1;
return 0;
}
and
qsort(static_cast<void*>(&vc[0]), vc.size(), sizeof(file_struct), CompDir);
getting something like that:
1 fhr(dir)
2 adm(dir)
3 054(dir)
4 rec (dir)
5 j02(dir)
6 gah
7 ttt
so i've sorted my array with directories and other files...
but now i'm searching for an possibility to sort the directory-elements and the file-elements in an alphabetical order which should lokk like similar to this way:
1 054 (dir)
2 adm(dir)
3 fhr(dir)
4 j02(dir)
5 rec (dir)
6 gah
7 ttt
i've tried it with std::sort and std::partial_sort but didn't get very far...
bool cn(const file_struct lhs, const file_struct rhs)
{
return lhs.strName.c_str() < rhs.strName.c_str();
}
sort(vc.begin(), vc.begin() + 5, cn);
normally i would do this with qsortand:
int CompName(const void* lhs, const void* rhs)
{
return _stricmp(((file_struct*)lhs)->strName.c_str(), ((file_struct*)rhs)->strName.c_str());
}
...so do i have to implement my struct as a class and do some operator work, in order to use the std::-sorting algos?
i would be very greatefull for any hints,
thanx in advance,
have a nice day,
ciao,
stefan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|