0

I have a list of windows but it is not in the order I want them. I'm able to get the windows into string from the title - they are being put into a list of windows. I want to sort this list in a specific order with Estimate 1st, Control Center 2nd, and Login 3rd. This is the order I desire. I have know idea on how to go about it but I want to sort it before it goes into the foreach loop.

    private void CloseMainWindows(IEnumerable<Window> Windows)
    {
        var winList = Windows.ToList();

        winList.Sort()//This is where I want to sort the list. 

        foreach (Window window in winList)
        {
            if (window.Title.Contains("Estimate"))
            {
                Estimate.closeEstimateWindow();
            }
            if (window.Title.Contains("Control Center"))
            {
                ContorlCenter.CloseContorlCenter();
            }
            if (window.Title.Contains("Login"))
            {
                login.ClickCanel();
            }
        } 
    }
7
  • 1
    Windows.OrderBy(...)? Commented Mar 19, 2015 at 18:39
  • 1
    The order of that foreach matters? Commented Mar 19, 2015 at 18:47
  • By the way you misspelled ContorlCenter. Commented Mar 19, 2015 at 18:49
  • Do you have any other information on your window class besides the name. Since the names are not in alphabetical order, an order by will not work. Commented Mar 19, 2015 at 18:53
  • 1
    @StuartSmith: OrderBy won't work directly on the name. But he knows the specific items, so he can transform the name to a scalar value. Commented Mar 19, 2015 at 18:56

2 Answers 2

3

One way would be to have a lookup function:

int GetTitleIndex(string s)
{
    if (s.Contains("Estimate")) return 0;
    if (s.Contains("Control Center")) return 1;
    if (s.Contains("Login")) return 2;
}

Then, to sort, you lookup the indexes:

winList.Sort((x, y) => GetTitleIndex(x).CompareTo(GetTitleIndex(y)));

Alternatively, you could create the list directly using LINQ's OrderBy:

var winList = Windows.OrderBy(GetTitleIndex).ToList();

And in fact in your case you don't even need the intermediate list:

foreach (var window in Windows.OrderBy(GetTitleIndex))
{
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

That you this is what I was looking for!
I would also appreciate if you up the question. I have no points what so ever.
0

You can do something like this :

List<Type> data = new List<Type>();
data.Sort(new Comparison<Type>(Compare));
private static int Compare(Type x, Type y)
{
   //you can compare them like so : 
   //I'll show you an example just for the sake of illustrating how : 
   if(x.Name.ToString().Length > y.Name.ToString().Length) return 1;
   else return -1;
   //the logic for the comparison is up to you.
   //compare the 2 elements.
}

2 Comments

Funny,because it doesn't compile because I did compare the two strings in a wrong way. The rest of the stuff is fine
Okay ... I retract my comment.

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.