- Home /
Order or Sort list of files using DirectoryInfo
I am using DirectoryInfo to get the files in a directory and list them with GUILayout. How do I sort the orders by date created? I think I may have found the answer through this link but I have no idea how to apply this solution? http://stackoverflow.com/questions/52842/sorting-directory-getfiles
Thanks!
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
var info2 = new DirectoryInfo(Application.persistentDataPath);
var fileInfo2 = info2.GetFiles();
for (file in fileInfo2) {
if (file.Extension == ".sav"){
GUILayout.BeginHorizontal("","selectionOutline");
if (GUILayout.Button (file.Name, "selectiontext" )) {
tempFile = Application.persistentDataPath+"/"+file.Name;
tempFileName = file.Name;
Loadconfirm = true;
}
GUILayout.Label("","spacer");
if (GUILayout.Button ("-Delete-", "deleteX")) {
tempFileName = file.Name;
tempFile = Application.persistentDataPath+"/"+file.Name;
Delconfirm = true;
}
GUILayout.EndHorizontal();
}
}
// End the scrollview we began above.
GUILayout.EndScrollView ();
The solution you point at requires C# (unless UnityScript has Linq). I can't tell from your code - is it written in C# or JS?
$$anonymous$$y script is JS....Is there a way in JS you could think of or point me in the direction of?
Answer by whydoidoit · Jul 21, 2012 at 12:07 PM
import System.Linq;
...
var fileInfo2 = info2.GetFiles().OrderBy(function(f) { return f.CreationTime; }).ToArray();
thank you so much!
Can I also ask how I might make it so that the newest is at the top of the list? Right now the newest ends up at the bottom.
Use OrderByDescending ins$$anonymous$$d of OrderBy. You can also do a multi key sort using ThenBy and ThenByDescending
So helpful thank you! Can you point me to a link for reading up on this type of array? Thanks again! Just found this for LINQ: http://www.dotnetperls.com/linq
That's the link I normally recommend, just Google .NET and the functions to see the standard docs.
You can use Linq on any array (or other enumerable) but be aware in Unity Script sometimes you need to convince it :)
For example:
var orderedChildren = transform.Cast.<Transform>().OrderBy(function(t) { return t.position.x; }).ToList();
That .Cast.() is sometimes necessary.
Your answer
Follow this Question
Related Questions
Instantiate a prefab from jsonarray? 1 Answer
Filled array becomes an empty one when I access it in another class 3 Answers
How can I check if an array slot of empty game objects is empty? 1 Answer
Method to loop through an unknown number of variable combinations in C# 1 Answer
Ordering an array of GO based on interfaced variables using linq. 1 Answer