0

I am sure I would do this, but the way I am thinking how to achieve this makes me sad, so I am asking for better way

List<Process> myList = new List<Process>();
Process[] processlist = Process.GetProcesses(); // Load all existing processes

// Pin existing sessions to the application
foreach (Process p in processlist)
{
    if (p.MainWindowTitle.Contains("TX")) // schema is like TX1 => TX10, but this loop is not sorted at all
    {
        myList.Add(p); // Unsorted list when looking by MainWindowTitle property
    }
}

Sorry fot nor precising the question about what kind of sorting I want to achieve
[0] TX1
[1] TX2
...
[5] TX6
etc.

Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
  • 2
    it's not even clear what kind of sorting are you talking about,what is the input and the expected output. – Selman Genç Jan 03 '15 at 18:47
  • if you get _TX1, TX10, TX2_ how do you want them sorted? – Steve Jan 03 '15 at 19:04
  • It is clear. The OP wants to sort by window title. What else can he say about it? – t3chb0t Jan 03 '15 at 19:22
  • @t3chb0t TX2 is before or after TX10? – Steve Jan 03 '15 at 19:33
  • @Steve there is actually never the case when TX10 should be before TX2 ;-) computer sort is quite usless for everything. – t3chb0t Jan 03 '15 at 19:40
  • Now it is a lot clearer. You need a Natural Sort where numbers inside strings are treated as numbers. You could [look at this answer](http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) or at this [blog post](http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/) – Steve Jan 03 '15 at 19:40
  • I used bubble sort and treated window title as int (sum of chars in string which should work fine), so the case is solved – Bartłomiej Sobieszek Jan 03 '15 at 21:42

3 Answers3

3

You could try something like this:

var myList = processlist.Where(p=>p.MainWindowTitle.Contains("TX"))
                        .OrderBy(p=>p.MainWindowTitle)
                        .ToList();
Christos
  • 53,228
  • 8
  • 76
  • 108
  • Could please the downvoter explain me where I am wrong? – Christos Jan 03 '15 at 18:49
  • I'm not a downvoter, but the question isn't clear at all. Your code is a sort of refactoring. If this is what OP wants, then it is OK... – Dennis Jan 03 '15 at 18:57
  • @Dennis this is not a refactoring. This is a possible answer to the sorting question. However as explained by Selman22 in its comment, we still don't know what is the sorting required (Downvote seems a bit excessive though) – Steve Jan 03 '15 at 18:58
  • To me the quesiton is clear enough and this answer seems to solve the problem. How is sorting by window title unclear? – t3chb0t Jan 03 '15 at 19:19
  • @t3chb0t - it won't solve the problem if they want TX10 to come after TX2, which you claim is very obvious and simple... You clearly do not understand the issues around sorting and the different ways it can be done. – Erik Funkenbusch Jan 03 '15 at 21:19
  • @ErikFunkenbusch no, of course, I don't understand them ;-) as I said in the other comment: who needs computer sorting? I've never seen a case where this would be usefull. And I didn't say it was simple. I said it was obvious. Not always the most complicated and generic algorithm that is able to sort _any_ kind of data is necessary if a simple one can solve a particular problem. See my answer below. – t3chb0t Jan 03 '15 at 21:30
  • Ok, I now see that I've missed the number after the TX when I wrote the first comment. In this case or course this answer does not solve it. – t3chb0t Jan 03 '15 at 22:09
1

How about using LINQ's OrderBy and a simple custom comparer. In this case this might be enough. From the information you gave us it should work for you.

class Program
{
    static void Main(string[] args)
    {
        var names = new string[] { "TX2", "TX12", "TX10", "TX3", "TX0" };
        var result = names.OrderBy(x => x, new WindowNameComparer()).ToList();
        // = TX0, TX2, TX3, TX10, TX13
    }
}

public class WindowNameComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        string pattern = @"TX(\d+)";
        var xNo = int.Parse(Regex.Match(x, pattern).Groups[1].Value);
        var yNo = int.Parse(Regex.Match(y, pattern).Groups[1].Value);
        return xNo - yNo;
    }
}

The WindowNameComparer reads (parses) the numbers attached to the TX and calculates the difference which is then used for sorting according to this table for the IComparer.Compare Method

Value              Meaning
Less than zero     x is less than y.
Zero               x equals y.
Greater than zero  x is greater than y.
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
-1

Well, I made this almost without linq, but I guess it's overkill

Process temp = null;
for (int i = 0; i < Games.Count; i++)
{
    for (int sort = 0; sort < Games.Count - 1; sort++)
    {
        string title1 = Games[sort].MainWindowTitle;
        string title2 = Games[sort+1].MainWindowTitle;

        int titleAsIntSum1 = title1.Sum(b => b); // This will work in this case
        int titleAsIntSum2 = title2.Sum(b => b);

        if (titleAsIntSum1 > titleAsIntSum2)
        {
            temp = Games[sort + 1];
            Games[sort + 1] = Games[sort];
            Games[sort] = temp;
        }
    }
}
Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
  • Yeah my bad for unclear question, but why I got downvote here, and not in first post? – Bartłomiej Sobieszek Jan 03 '15 at 21:38
  • There was already one answer posted together with few comments on your question that you didn't answer.Not to mention your code doesn't really explain the issue. What's the point of asking unclear question and giving unclear answer ? – tchrikch Jan 03 '15 at 21:38