Search code examples
arrayssortingwindows-phone-8splitisolatedstorage

Sort an Array, populated by an IsolatedStorageFile text file seems to move the 0 entry


I have the code below that splits a text file from IsolatedStorage, populates an Array with the data, sorts it, and then assigns it as the source for a ListPicker:

var splitFile = fileData.Split(';');
string[] testArray = splitFile;
Array.Sort<string>(testArray);
testLocationPicker.ItemsSource = testArray;

However, it doesn't seem to populating the array correctly and the sorting doesn't appear to be working as expected either.

The testArray[0] is blank, when it should be populated. When the output is shown the entry that should be at [0] appears at the bottom.

BEFORE SORTING:

enter image description here

AFTER SORTING:

enter image description here

It's only in sorting the array that it seems to screw up the order. UPDATE: I tried the suggested:

var splitFile = fileData.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
string[] testArray = splitFile;
Array.Sort<string>(testArray);
testLocationPicker.ItemsSource = testArray;

This still results in the second screenshot, above.

When the app first ever runs I do this:

 StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
                var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
                store.CreateDirectory("testLocations");                                 // Create a directory
                IsolatedStorageFileStream rootFile = store.CreateFile("locations.txt"); // Create a file in the root.
                rootFile.Close();                                                       // Close File
                string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
                Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)

                string filePath = "locations.txt";

                if (store.FileExists(filePath)) {

                    Debug.WriteLine("Files Exists"); 
                    StreamWriter sw =
                            new StreamWriter(store.OpenFile(filePath,
                                FileMode.Open, FileAccess.Write));

                            Debug.WriteLine("Writing..."); 
                            sw.WriteLine("Chicago, IL;");
                            sw.WriteLine("Chicago, IL (Q);");
                            sw.WriteLine("Dulles, VA;");
                            sw.WriteLine("Dulles, VA (Q);");
                            sw.WriteLine("London, UK;");
                            sw.WriteLine("London, UK (Q);");
                            sw.WriteLine("San Jose, CA;");
                            sw.WriteLine("San Jose, CA (Q);");
                            sw.Close();
                            Debug.WriteLine("Writing complete"); 

                        }

Then when I add to the file I do this:

StringBuilder sb = new StringBuilder();                                 // Use a StringBuilder to construct output.
            var store = IsolatedStorageFile.GetUserStoreForApplication();           // Create a store
            string[] filesInTheRoot = store.GetFileNames();                         // Store all files names in an array
            Debug.WriteLine(filesInTheRoot[0]);                                     // Show first file name retrieved (only one stored at the moment)
            byte[] data = Encoding.UTF8.GetBytes(locationName + ";");
            string filePath = "locations.txt";

            if (store.FileExists(filePath))
            {
                using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Append, store))
                {

                    Debug.WriteLine("Writing...");
                    stream.Write(data, 0, data.Length);   // Semi Colon required for location separation in text file
                    stream.Close();
                    Debug.WriteLine(locationName + "; added");
                    Debug.WriteLine("Writing complete");
                }


            }

I'm splitting using a ";" could this be an issue?


Solution

  • I went a different way using IsolatedStorageSetting and storing Arrays/Lists to do what I wanted.