Improve: three-state online host list sorting

Clicking the same column header now cycles ascending -> descending ->
restore default order instead of only toggling direction. The default order
is the host connection order, reconstructed by sorting on online time
(descending alive time) so it no longer depends on the in-memory append
order surviving an earlier sort. The reset preference is persisted as an
empty value so it survives a restart.

Co-Authored-By: deepseek-v4-pro
This commit is contained in:
yuanyuanxiang
2026-08-29 16:29:49 +02:00
parent 35dc2d89a0
commit 52b4afb8cf
2 changed files with 32 additions and 17 deletions

View File

@@ -4044,8 +4044,13 @@ void CMy2015RemoteDlg::SortByColumn(int nColumn)
if (ColumnToDataSlot(nColumn) < 0) return;
if (nColumn == m_nSortColumn) {
// 如果点击的是同一列,切换排序顺序
m_bSortAscending = !m_bSortAscending;
// 同一列三态循环:升序 -> 降序 -> 恢复默认顺序 -> 升序 ...
if (m_bSortAscending) {
m_bSortAscending = false; // 升序 -> 降序
} else {
m_nSortColumn = -1; // 降序 -> 恢复默认(按上线先后)
m_bSortAscending = true;
}
} else {
// 否则,切换到新列并设置为升序
m_nSortColumn = nColumn;
@@ -4065,22 +4070,29 @@ void CMy2015RemoteDlg::SortByColumn(int nColumn)
void CMy2015RemoteDlg::ApplySort()
{
// 无有效排序偏好或列不合法(缩略图/越界)时保持列表原始顺序
// 虚拟列表:对数据源进行排序,调用方需持有 m_cs
if (m_nSortColumn <= COL_THUMBNAIL || m_nSortColumn >= g_Column_Count_Online) {
return;
// 无有效排序偏好(缩略图/越界/已清零):恢复默认顺序。
// 默认顺序 = 上线先后 = 上线时间(OnlineTime)升序 = 在线时长(GetAliveTime)降序。
std::sort(m_HostList.begin(), m_HostList.end(),
[](context* a, context* b) {
uint64_t ta = a ? a->GetAliveTime() : 0;
uint64_t tb = b ? b->GetAliveTime() : 0;
return ta > tb;
});
} else {
// 按指定列排序(列号映射到底层数据槽)
int slot = ColumnToDataSlot(m_nSortColumn);
bool asc = m_bSortAscending;
std::sort(m_HostList.begin(), m_HostList.end(),
[slot, asc](context* a, context* b) {
CString s1 = a ? a->GetClientData(slot) : "";
CString s2 = b ? b->GetClientData(slot) : "";
int result = s1.Compare(s2);
return asc ? (result < 0) : (result > 0);
});
}
// 虚拟列表:对数据源进行排序(列号映射到底层数据槽),调用方需持有 m_cs
int slot = ColumnToDataSlot(m_nSortColumn);
bool asc = m_bSortAscending;
std::sort(m_HostList.begin(), m_HostList.end(),
[slot, asc](context* a, context* b) {
CString s1 = a ? a->GetClientData(slot) : "";
CString s2 = b ? b->GetClientData(slot) : "";
int result = s1.Compare(s2);
return asc ? (result < 0) : (result > 0);
});
// 排序后 clientID -> 索引映射已改变,需重建
m_ClientIndex.clear();
for (size_t i = 0; i < m_HostList.size(); ++i) {
@@ -4111,7 +4123,10 @@ void CMy2015RemoteDlg::LoadSortPreference()
void CMy2015RemoteDlg::SaveSortPreference()
{
std::string val = std::to_string(m_nSortColumn) + "," + (m_bSortAscending ? "1" : "0");
// 清零(恢复默认顺序)时写入空值,等价于"无偏好",下次启动走 LoadSortPreference 的空分支
std::string val = (m_nSortColumn >= 0)
? std::to_string(m_nSortColumn) + "," + (m_bSortAscending ? "1" : "0")
: "";
THIS_CFG.SetStr("list", "OnlineListSort", val);
}

View File

@@ -173,7 +173,7 @@ protected:
DECLARE_MESSAGE_MAP()
public:
void SortByColumn(int nColumn);
void ApplySort(); // 按偏好重排 m_HostList调用方需持有 m_cs
void ApplySort(); // 按偏好重排 m_HostList;无偏好时恢复默认顺序(上线先后)(调用方需持有 m_cs
void LoadSortPreference();
void SaveSortPreference();
afx_msg VOID OnHdnItemclickList(NMHDR* pNMHDR, LRESULT* pResult);