0

hoping you can help me with this.

Okay I have two lists.

List1 - Gets the names of files without a path (so I can list the names on dynamically created buttons). More so for visual purposes only.

List2 - Stores the actual path to the file.

Now I have done List1.Sort(); and List2.Sort(). But being they are alphabetical, A direct path from C:/// is different than Hello.png. <(Just an example name)>

So the problem I am facing is, List1 and 2 aren't identical in where the actual elements are stored. (Which does make sense).

So is there a way to like sort List2 to match the same order as List1? Or vice versa.. So when I click the button, it loads the proper image,etc that it needs to.

8
  • 4
    Why have two lists in the first place? If they each refer to the "same thing" (file on disk, only difference is full vs. filename), then I would put them into a single data structure, possibly List<Tuple<string, string>> where tuple Item1 is full path and item 2 is filename. Or, just save the full path into one list and rely in Path.GetFilename() to get the filename. Commented Sep 23, 2016 at 17:35
  • Why not using a Tuple or a custom class? Commented Sep 23, 2016 at 17:35
  • 1
    Simple: What you're doing is a bad idea. Either make a single list of objects which have Name and FullPath properties, or keep a single list of full paths and extract the names with Path.GetFileName(path) as needed. Don't do this at all: Solved. Commented Sep 23, 2016 at 17:36
  • why are you using two lists to begin with? you can simply just do Path.GetFileNameWithoutExtension(path); or Path.GetFileName(path); on items of your list to get the name of the file. Commented Sep 23, 2016 at 17:36
  • Use List<Dictionary<string,string>> instead of two lists Commented Sep 23, 2016 at 17:38

2 Answers 2

1

I agree with the comments above, use a single list of full paths and use the System.Io.Path.FileName method to order by file name regardless of directory.

        var list2 = new List<string>() { @"C:\Directory1\B.txt", @"C:\Directory2\A.txt" };
        var orderedList = list2.OrderBy(System.IO.Path.GetFileName);
        //orderedList[0] is @"C:\Directory2\A.txt"
        //orderedList[1] is @"C:\Directory1\B.txt"
Sign up to request clarification or add additional context in comments.

1 Comment

That is now what I am doing, This just completely blew over my head and I can't believe it.... I've been so stressed out over this massive system I've made that it caused me to forget one of the most basic things. I will mark as correct as soon as it allows me. Thanks so much guys!
0

Create a sorted dictionary by zipping the two lists.

var sortedDict = new SortedDictionary<string, string>(
        list1.Zip(ist2, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.