Codechange: use std::sort() to sort file lists

This commit is contained in:
glx 2019-04-11 22:52:41 +02:00 committed by PeterN
parent b52561fd38
commit 5b77102b63
3 changed files with 12 additions and 15 deletions

View File

@ -47,21 +47,20 @@ extern void GetOldSaveGameName(const char *file, char *title, const char *last);
/** /**
* Compare two FiosItem's. Used with sort when sorting the file list. * Compare two FiosItem's. Used with sort when sorting the file list.
* @param da A pointer to the first FiosItem to compare. * @param other The FiosItem to compare to.
* @param db A pointer to the second FiosItem to compare. * @return for ascending order: returns true if da < db. Vice versa for descending order.
* @return -1, 0 or 1, depending on how the two items should be sorted.
*/ */
int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db) bool FiosItem::operator< (const FiosItem &other) const
{ {
int r = 0; bool r = false;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) { if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
r = da->mtime < db->mtime ? -1 : 1; r = (*this).mtime < other.mtime;
} else { } else {
r = strcasecmp(da->title, db->title); r = strcasecmp((*this).title, other.title) < 0;
} }
if (_savegame_sort_order & SORT_DESCENDING) r = -r; if (_savegame_sort_order & SORT_DESCENDING) r = !r;
return r; return r;
} }
@ -380,7 +379,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
{ {
SortingBits order = _savegame_sort_order; SortingBits order = _savegame_sort_order;
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
QSortT(file_list.files.data(), file_list.files.size(), CompareFiosItems); std::sort(file_list.files.begin(), file_list.files.end());
_savegame_sort_order = order; _savegame_sort_order = order;
} }
@ -395,7 +394,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
scanner.Scan(nullptr, subdir, true, true); scanner.Scan(nullptr, subdir, true, true);
} }
QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems); std::sort(file_list.files.begin() + sort_start, file_list.files.end());
/* Show drives */ /* Show drives */
FiosGetDrives(file_list); FiosGetDrives(file_list);

View File

@ -107,6 +107,7 @@ struct FiosItem {
uint64 mtime; uint64 mtime;
char title[64]; char title[64];
char name[MAX_PATH]; char name[MAX_PATH];
bool operator< (const FiosItem &other) const;
}; };
/** List of file information. */ /** List of file information. */
@ -227,6 +228,4 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last);
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last); FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last);
int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);
#endif /* FIOS_H */ #endif /* FIOS_H */

View File

@ -260,8 +260,7 @@ static void SortSaveGameList(FileList &file_list)
} }
} }
size_t s_amount = file_list.Length() - sort_start - sort_end; std::sort(file_list.files.begin() + sort_start, file_list.files.end() - sort_end);
QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems);
} }
struct SaveLoadWindow : public Window { struct SaveLoadWindow : public Window {