Sort columns with numbers correctly

Fixes #618
Closes #657 (Thanks @ViCrack)
This commit is contained in:
MaxXor 2023-02-11 17:33:53 +01:00
parent 16a4702d0e
commit 8d3b84e950
9 changed files with 104 additions and 22 deletions

View File

@ -14,7 +14,7 @@ internal class AeroListView : ListView
private const short UISF_HIDEFOCUS = 0x1;
private readonly IntPtr _removeDots = new IntPtr(NativeMethodsHelper.MakeWin32Long(UIS_SET, UISF_HIDEFOCUS));
private ListViewColumnSorter LvwColumnSorter { get; set; }
public ListViewColumnSorter LvwColumnSorter { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="AeroListView"/> class.

View File

@ -87,6 +87,7 @@ private void InitializeComponent()
this.lstConnections.TabIndex = 0;
this.lstConnections.UseCompatibleStateImageBehavior = false;
this.lstConnections.View = System.Windows.Forms.View.Details;
this.lstConnections.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.lstConnections_ColumnClick);
//
// columnHeader1
//

View File

@ -160,5 +160,10 @@ private void closeConnectionToolStripMenuItem_Click(object sender, EventArgs e)
_connectionsHandler.RefreshTcpConnections();
}
}
private void lstConnections_ColumnClick(object sender, ColumnClickEventArgs e)
{
lstConnections.LvwColumnSorter.NeedNumberCompare = (e.Column == 2 || e.Column == 4);
}
}
}

View File

@ -332,6 +332,7 @@ private void InitializeComponent()
this.lstDirectory.DragDrop += new System.Windows.Forms.DragEventHandler(this.lstDirectory_DragDrop);
this.lstDirectory.DragEnter += new System.Windows.Forms.DragEventHandler(this.lstDirectory_DragEnter);
this.lstDirectory.DoubleClick += new System.EventHandler(this.lstDirectory_DoubleClick);
this.lstDirectory.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.lstDirectory_ColumnClick);
//
// hName
//

View File

@ -250,7 +250,6 @@ private string NavigateUp()
private void FrmFileManager_Load(object sender, EventArgs e)
{
this.Text = WindowHelper.GetWindowTitle("File Manager", _connectClient);
_fileManagerHandler.RefreshDrives();
}
@ -269,9 +268,9 @@ private void lstDirectory_DoubleClick(object sender, EventArgs e)
{
if (lstDirectory.SelectedItems.Count > 0)
{
FileType type = (FileType) lstDirectory.SelectedItems[0].Tag;
FileManagerListTag tag = (FileManagerListTag) lstDirectory.SelectedItems[0].Tag;
switch (type)
switch (tag.Type)
{
case FileType.Back:
SwitchDirectory(NavigateUp());
@ -283,13 +282,18 @@ private void lstDirectory_DoubleClick(object sender, EventArgs e)
}
}
private void lstDirectory_ColumnClick(object sender, ColumnClickEventArgs e)
{
lstDirectory.LvwColumnSorter.NeedNumberCompare = (e.Column == 1);
}
private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem files in lstDirectory.SelectedItems)
{
FileType type = (FileType)files.Tag;
FileManagerListTag tag = (FileManagerListTag)files.Tag;
if (type == FileType.File)
if (tag.Type == FileType.File)
{
string remotePath = GetAbsolutePath(files.SubItems[0].Text);
@ -324,9 +328,9 @@ private void executeToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem files in lstDirectory.SelectedItems)
{
FileType type = (FileType) files.Tag;
FileManagerListTag tag = (FileManagerListTag) files.Tag;
if (type == FileType.File)
if (tag.Type == FileType.File)
{
string remotePath = GetAbsolutePath(files.SubItems[0].Text);
@ -339,9 +343,9 @@ private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem files in lstDirectory.SelectedItems)
{
FileType type = (FileType)files.Tag;
FileManagerListTag tag = (FileManagerListTag)files.Tag;
switch (type)
switch (tag.Type)
{
case FileType.Directory:
case FileType.File:
@ -351,7 +355,7 @@ private void renameToolStripMenuItem_Click(object sender, EventArgs e)
if (InputBox.Show("New name", "Enter new name:", ref newName) == DialogResult.OK)
{
newName = GetAbsolutePath(newName);
_fileManagerHandler.RenameFile(path, newName, type);
_fileManagerHandler.RenameFile(path, newName, tag.Type);
}
break;
}
@ -367,14 +371,14 @@ private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem files in lstDirectory.SelectedItems)
{
FileType type = (FileType)files.Tag;
FileManagerListTag tag = (FileManagerListTag)files.Tag;
switch (type)
switch (tag.Type)
{
case FileType.Directory:
case FileType.File:
string path = GetAbsolutePath(files.SubItems[0].Text);
_fileManagerHandler.DeleteFile(path, type);
_fileManagerHandler.DeleteFile(path, tag.Type);
break;
}
}
@ -385,9 +389,9 @@ private void addToStartupToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem files in lstDirectory.SelectedItems)
{
FileType type = (FileType)files.Tag;
FileManagerListTag tag = (FileManagerListTag)files.Tag;
if (type == FileType.File)
if (tag.Type == FileType.File)
{
string path = GetAbsolutePath(files.SubItems[0].Text);
@ -413,9 +417,9 @@ private void openDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
if (lstDirectory.SelectedItems.Count == 1)
{
var item = lstDirectory.SelectedItems[0];
FileType type = (FileType)item.Tag;
FileManagerListTag tag = (FileManagerListTag)item.Tag;
if (type == FileType.Directory)
if (tag.Type == FileType.Directory)
{
path = GetAbsolutePath(item.SubItems[0].Text);
}
@ -513,7 +517,7 @@ private void AddItemToFileBrowser(string name, long size, FileType type, int ima
(type != FileType.Back) ? type.ToString() : string.Empty
})
{
Tag = type,
Tag = new FileManagerListTag(type, size),
ImageIndex = imageIndex
};

View File

@ -119,6 +119,7 @@ private void InitializeComponent()
this.lstTasks.TabIndex = 1;
this.lstTasks.UseCompatibleStateImageBehavior = false;
this.lstTasks.View = System.Windows.Forms.View.Details;
this.lstTasks.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.lstTasks_ColumnClick);
//
// hProcessname
//

View File

@ -157,5 +157,10 @@ private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
_taskManagerHandler.RefreshProcesses();
}
private void lstTasks_ColumnClick(object sender, ColumnClickEventArgs e)
{
lstTasks.LvwColumnSorter.NeedNumberCompare = (e.Column == 1);
}
}
}

View File

@ -0,0 +1,17 @@
using Quasar.Common.Enums;
namespace Quasar.Server.Models
{
public class FileManagerListTag
{
public FileType Type { get; set; }
public long FileSize { get; set; }
public FileManagerListTag(FileType type, long fileSize)
{
this.Type = type;
this.FileSize = fileSize;
}
}
}

View File

@ -1,4 +1,5 @@
using System.Collections;
using Quasar.Server.Models;
using System.Collections;
using System.Windows.Forms;
namespace Quasar.Server.Utilities
@ -20,6 +21,11 @@ public class ListViewColumnSorter : IComparer
/// </summary>
private readonly CaseInsensitiveComparer _objectCompare;
/// <summary>
/// Specifies if number or text comparision is needed
/// </summary>
private bool _needNumberCompare;
/// <summary>
/// Class constructor. Initializes various elements
/// </summary>
@ -33,6 +39,8 @@ public ListViewColumnSorter()
// Initialize the CaseInsensitiveComparer object
_objectCompare = new CaseInsensitiveComparer();
_needNumberCompare = false;
}
/// <summary>
@ -51,8 +59,39 @@ public int Compare(object x, object y)
return 0;
// Compare the two items
var compareResult = _objectCompare.Compare(listviewX.SubItems[_columnToSort].Text,
listviewY.SubItems[_columnToSort].Text);
int compareResult;
if (_needNumberCompare)
{
long a, b;
if (listviewX.Tag is FileManagerListTag)
{
// fileSize to be compared
a = (listviewX.Tag as FileManagerListTag).FileSize;
b = (listviewY.Tag as FileManagerListTag).FileSize;
compareResult = a >= b ? (a == b ? 0 : 1) : -1;
}
else
{
if (long.TryParse(listviewX.SubItems[_columnToSort].Text, out a)
&& long.TryParse(listviewY.SubItems[_columnToSort].Text, out b))
{
compareResult = a >= b ? (a == b ? 0 : 1) : -1;
}
else
{
compareResult = _objectCompare.Compare(listviewX.SubItems[_columnToSort].Text,
listviewY.SubItems[_columnToSort].Text);
}
}
}
else
{
compareResult = _objectCompare.Compare(listviewX.SubItems[_columnToSort].Text,
listviewY.SubItems[_columnToSort].Text);
}
// Calculate correct return value based on object comparison
if (_orderOfSort == SortOrder.Ascending)
@ -89,5 +128,14 @@ public SortOrder Order
set { _orderOfSort = value; }
get { return _orderOfSort; }
}
/// <summary>
/// Specifies if number or text comparision is needed.
/// </summary>
public bool NeedNumberCompare
{
set { _needNumberCompare = value; }
get { return _needNumberCompare; }
}
}
}