MCPcopy Create free account
hub / github.com/dfranx/ImFileDialog / m_sortContent

Method m_sortContent

ImFileDialog.cpp:1001–1059  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

999 m_refreshIconPreview();
1000 }
1001 void FileDialog::m_sortContent(unsigned int column, unsigned int sortDirection)
1002 {
1003 // 0 -> name, 1 -> date, 2 -> size
1004 m_sortColumn = column;
1005 m_sortDirection = sortDirection;
1006
1007 // split into directories and files
1008 std::partition(m_content.begin(), m_content.end(), [](const FileData& data) {
1009 return data.IsDirectory;
1010 });
1011
1012 if (m_content.size() > 0) {
1013 // find where the file list starts
1014 size_t fileIndex = 0;
1015 for (; fileIndex < m_content.size(); fileIndex++)
1016 if (!m_content[fileIndex].IsDirectory)
1017 break;
1018
1019 // compare function
1020 auto compareFn = [column, sortDirection](const FileData& left, const FileData& right) -> bool {
1021 // name
1022 if (column == 0) {
1023 std::string lName = left.Path.u8string();
1024 std::string rName = right.Path.u8string();
1025
1026 std::transform(lName.begin(), lName.end(), lName.begin(), ::tolower);
1027 std::transform(rName.begin(), rName.end(), rName.begin(), ::tolower);
1028
1029 int comp = lName.compare(rName);
1030
1031 if (sortDirection == ImGuiSortDirection_Ascending)
1032 return comp < 0;
1033 return comp > 0;
1034 }
1035 // date
1036 else if (column == 1) {
1037 if (sortDirection == ImGuiSortDirection_Ascending)
1038 return left.DateModified < right.DateModified;
1039 else
1040 return left.DateModified > right.DateModified;
1041 }
1042 // size
1043 else if (column == 2) {
1044 if (sortDirection == ImGuiSortDirection_Ascending)
1045 return left.Size < right.Size;
1046 else
1047 return left.Size > right.Size;
1048 }
1049
1050 return false;
1051 };
1052
1053 // sort the directories
1054 std::sort(m_content.begin(), m_content.begin() + fileIndex, compareFn);
1055
1056 // sort the files
1057 std::sort(m_content.begin() + fileIndex, m_content.end(), compareFn);
1058 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected