Link to home
Create AccountLog in
Avatar of frtools
frtoolsFlag for United States of America

asked on

Sorting a List of String arrays based on a list of second string arrays

What I'm actually trying to sort is location of items in a warehouse.

I have a program that reads a .csv file and adds them to a list of string arrays.
I have another array of strings that contain what i want to order my list by.

Here's what I have so far..
 
   List<string[]> ItemList = new List<string[]>();
   List<string[]> TempItemList = new List<string[]>();
   string[] OrderedLocationList= { "A", "B", "C", "D", "E};

I have a function that populates the TempItemList<>
and that works everything is perfect there but I want to reorder the list so that its in the order of the OrderedLocationList[]. By the way that list will look alot different on how I want it ordered.

What I have for the function to sort the list is this..

private void SortList()
{
       foreach (string LocPrefix in OrderedLocationList)
        {
                foreach (string[] location in TempItemList)
                {
                    if (location[1].Contains(LocPrefix))
                    {
                        ItemList.Add(TempItemList[]);
                    }
                }
            }
        }
Now when the if statement contains the OrderedLocationList string i want it to add it to the ItemList<>, then delete it from the TempItemList<>, but .Add(TempItemList[]) wants an index. I hate asking for answers but this has got me stumped so any help or where to find help is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The above solution "logic" is correct but you need to change some code.

Here removing the item from TempItemList in the loop itself. it will gives you an error as we can not modify the list when we are acccessing the List object.

To avoid this, create a dummytemplist just replica of TempItemList.

Then instead of writing

TempItemList.RemoveAt(i);

Just change the code

 dummytemplist.RemoveAt(i);

>>  Here removing the item from TempItemList in the loop itself. it will gives you an error as we can not modify the list when we are acccessing the List object.

To clarify, you cannot modify the list when working with a foreach loop. You can modify the list if you use a for loop. There is no need to create a replica list.
Avatar of frtools

ASKER

Thanks for the quick responses, Happy New Year by the way, the loop only seems to reverse the list
Avatar of frtools

ASKER

Ok here is what I came up with..

There is an infinite loop can't figure out where.
while (TempItemList.Count - 1 >= 0)
            {
                for (int i = 0 ; i >= OrderedLocationList.Count()-1; i++)
                {
                    for (int x = TempItemList.Count - 1; x >= 0; x--)
                    {
                        if (TempItemList[x][2].Contains(','))
                        {
                            multiLocations = TempItemList[x][1].Split(',');
                            foreach (string loc in multiLocations)
                            {
                                if (loc.StartsWith(OrderedLocationList[i]))
                                {
                                    ItemList.Add(TempItemList[x]);
                                    TempItemList.RemoveAt(x);
                                    break;
                                }
                            }
                        }
                        else if (TempItemList[x][1].StartsWith(OrderedLocationList[i]))
                        {
                            ItemList.Add(TempItemList[x]);
                            TempItemList.RemoveAt(x);

                            break;
                        }

                    }
                }
            }

Open in new window

Happy New Year to you as well   :)

To maintain the order, use Insert instead of Add:
private void SortList()
{
    for (int i = TempItemList.Count - 1; i >= 0; i--)
    {
        foreach (string LocPrefix in OrderedLocationList)
        {
            if (TempItemList[i][1].Contains(LocPrefix))
            {
                ItemList.Insert(0, TempItemList[i]);
                TempItemList.RemoveAt(i);

                break;
            }
        }
    }
}

Open in new window

Avatar of frtools

ASKER

Replaced..

i >= OrderedLocationList.Count()-1

With

i >= OrderedLocationList.Count()-1

Thanks for the help.
Avatar of frtools

ASKER

Needed to reverse the first for loop.
Glad to help  :)