This project is read-only.

Sort Column based on another column

May 20 at 3:56 PM
Edited May 20 at 4:02 PM
In Razor view, there is a grid where I display the File Size in B, KB, MB, GB etc. I want to sort this column in ASC/DESC order. Since this is a calculated string column, sorting in correct order can't be achieved. In the model class, I have a property 'Size' which receives the Size in bytes and a calculated property 'SizeString' to calculate the szie in B, KB, MB, GB etc.

How can I sort the 'SizeString' column correctly in ASC/DESC order based on Size?

Model class is as below:-
public class FileStore
        public string Name { get; set; }
        public DateTime LastModified { get; set; }
        public decimal Size { get; set; }
        public string SizeString
                string[] sizes = { "B", "KB", "MB", "GB", "TB" };
                int order = 0;
                while (Size >= 1024 && order < sizes.Length - 1)
                    Size = Size / 1024;
                return string.Format("{0:0.##} {1}", Size, sizes[order]);
View Code is as below:-
@Html.Grid(Model).Columns(columns =>
        columns.Add(c => c.Name).Titled("File Name").Filterable(true).Sortable(true);

        columns.Add(c => c.LastModified).Titled("Last Modified").Sortable(true);

        @*columns.Add(c => c.Size).Titled("File Size (KiloBytes)");*@

        columns.Add(c => c.SizeString).Titled("File Size").Sortable(false);

.Add().Encoded(false).Sanitized(false).SetWidth(100).RenderValueAs(o => Html.NoEncodeActionLink("<span class='glyphicon glyphicon-trash'></span>",
            "Delete File", "Delete", "Explore", new { @fileName = o.Name },
            new { @class = "btn btn-sm btn-danger", onclick = "return confirm('Are you sure you wish to delete this File?');" }));