Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseThe comparison to use when using ColumnSortingMode.Default. Compares two items by their index in the source.
The following example creates a MultiColumnListView that can be sorted with the default algorithm:
using System; using System.Collections.Generic; using System.IO; using System.Text; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; using Debug = UnityEngine.Debug;
namespace MultiColumnViewExample { public class FileExplorerWindow : EditorWindow { MultiColumnListView m_ListView; List<FileInfoWrapper> m_Source = new List<FileInfoWrapper>(); StringBuilder m_StringBuilder = new StringBuilder();
[MenuItem("MultiColumnView Examples/File Explorer (List)")] public static void ShowExample() { var wnd = GetWindow<FileExplorerWindow>(); wnd.titleContent = new GUIContent("File Explorer"); }
public void CreateGUI() { // Here we're setting up a 3-column list view to display information on all files in the project. // Each column will be sorted using the `comparison` callback. // Use CTRL/CMD + click on a column header to sort multiple columns. // Use Shift + click on a column header to remove sorting. m_ListView = new MultiColumnListView(new Columns() { new Column() { name = "filename", title = "Name", minWidth = 600, bindCell = (ve, row) => ((Label)ve).text = m_Source[row].fullFileName, comparison = (a, b) => m_Source[a].fullFileName.CompareTo(m_Source[b].fullFileName), }, new Column() { name = "type", title = "Type", minWidth = 60, bindCell = (ve, row) => ((Label)ve).text = m_Source[row].type, comparison = (a, b) => m_Source[a].type.CompareTo(m_Source[b].type), }, new Column() { name = "size", title = "Size", minWidth = 60, bindCell = (ve, row) => ((Label)ve).text = m_Source[row].displaySize, comparison = (a, b) => m_Source[a].sizeLong.CompareTo(m_Source[b].sizeLong), } });
// This causes the list view to take all the space available. m_ListView.style.flexGrow = 1;
// Setting the sortingMode to Default enables Unity's sorting algorithm, which will use the Columns' comparison callbacks. m_ListView.sortingMode = ColumnSortingMode.Default;
// Use all the files in the project as the source. foreach (var file in Directory.EnumerateFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)) { m_Source.Add(new FileInfoWrapper(new FileInfo(file))); }
m_ListView.itemsSource = m_Source;
// The columnSortingChanged and sortedColumns property can be used to know when the ordering changes. // This is the callback to use if you want to implement a custom sorting algorithm, when using ColumnSortingMode.Custom. m_ListView.columnSortingChanged += () => { m_StringBuilder.Clear(); foreach (var s in m_ListView.sortedColumns) { m_StringBuilder.Append($"Sorted {s.columnName} in {s.direction} order."); }
Debug.Log(m_StringBuilder.ToString()); };
rootVisualElement.Add(m_ListView); } }
public class FileInfoWrapper { static readonly string[] k_SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
static string GetBytesReadable(long fileSizeInBytes) { if (fileSizeInBytes == 0) return "0" + k_SizeSuffixes[0];
var magnitude = (int)Math.Log(fileSizeInBytes, 1024); var adjustedSize = fileSizeInBytes / (1L << (magnitude * 10));
return $"{adjustedSize:n1} {k_SizeSuffixes[magnitude]}"; }
public FileInfoWrapper(FileInfo fileInfo) { fullFileName = fileInfo.FullName; type = fileInfo.Extension.Substring(1).ToUpper(); displaySize = GetBytesReadable(fileInfo.Length); sizeLong = fileInfo.Length; }
public readonly string fullFileName; public readonly string type; public readonly string displaySize; public readonly long sizeLong; // It is faster to compare long than strings. } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.