0

Each array string represents a text file in a folder. I want the array to be sorted based on what the text file contains. How would I do that?

1
  • First you need to look at whats in each file. Loop through the strings, look at the file, and figure out what position it should be in. That should get you started. Commented Mar 31, 2012 at 20:09

1 Answer 1

5

You can pass a custom Comparison to Array.Sort, so just read the file there:

Array.Sort(str, Function(a, b)
                    Dim aContents As String = IO.File.ReadAllText(a)
                    Dim bContents As String = IO.File.ReadAllText(b)

                    'Compare the contents and return -1 if a < b, 0 if a = b, or 1 if a > b.
                End Function)

If efficiency is an issue there, you may want to cache the contents of each file in a Dictionary or do something similar.


Also, you can go LINQ, depending on what exactly it is in the files you need to sort by:

Dim result = str.
    Select(Function(x) New With {.File = x, .Contents = IO.File.ReadAllText(x)}).
    OrderBy(Function(y) y.Contents)

... for example.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.