Home All Groups Group Topic Archive Search About
Author
26 Nov 2007 9:13 AM
Ole
Hi,

I'm looking for an easy way to sort the string array returned by
"SerialPort.GetPortNames()" so that the names are in the correct numeric
order instead of alphabetic order:

COM1
COM2
COM10

Instead of:

COM1
COM11
COM2

That is wahat is returned by the Array.Sort method.

Is there an easy way to obtain that?

Thanks
Ole

Author
26 Nov 2007 9:24 AM
Lasse_Vågsæther_Karlsen
Ole wrote:
> Hi,
>
> I'm looking for an easy way to sort the string array returned by
> "SerialPort.GetPortNames()" so that the names are in the correct numeric
> order instead of alphabetic order:

I have a class that implements that sorting, but I hope someone will
come to your rescue and point to a .NET runtime way, and make my class
obsolete.

If not, give me a holler and I'll send you a copy.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Author
26 Nov 2007 2:53 PM
Brian Gideon
Show quote
On Nov 26, 3:13 am, "Ole" <o***@blabla.com> wrote:
> Hi,
>
> I'm looking for an easy way to sort the string array returned by
> "SerialPort.GetPortNames()" so that the names are in the correct numeric
> order instead of alphabetic order:
>
> COM1
> COM2
> COM10
>
> Instead of:
>
> COM1
> COM11
> COM2
>
> That is wahat is returned by the Array.Sort method.
>
> Is there an easy way to obtain that?
>
> Thanks
> Ole

Right your own IComparer and use one of the overloads of Array.Sort
that takes the IComparer as a parameter.
Author
26 Nov 2007 2:59 PM
Brian Gideon
On Nov 26, 8:53 am, Brian Gideon <briangid***@yahoo.com> wrote:
> Right your own IComparer and use one of the overloads of Array.Sort
> that takes the IComparer as a parameter.

That's embarrassing.  I meant to say "Write you own..."
Author
26 Nov 2007 6:25 PM
Peter Duniho
On 2007-11-26 06:53:21 -0800, Brian Gideon <briangid***@yahoo.com> said:

> Right your own IComparer and use one of the overloads of Array.Sort
> that takes the IComparer as a parameter.

As an alternative, you can use an anonymous method, passing it as a
Comparison<T> to Array.Sort().

Writing an IComparer would be the best option if it's something you'd
need to use in multiple places.  I think an anonymous method is nicer
if there's just the one place you need it, rather than creating a whole
new class just for that purpose.

It just depends on the OP's needs.

As an example of both, for the OP's benefit:

    class OrderNumeric : IComparer<string>
    {
        public int Compare(string strA, string strB)
        {
            int idA = int.Parse(strA.Substring(3)),
                idB = int.Parse(strB.Substring(3));

            return idA.CompareTo(idB);
        }
    }

    void SortComPorts(string[] rgstrPorts)
    {
        Array.Sort<string>(rgstrPorts, new OrderNumeric());
    }


    void SortComPorts(string[] rgstrPorts)
    {
        Array.Sort<string>(rgstrPorts, delegate (string strA, string strB)
            {
                int idA = int.Parse(strA.Substring(3)),
                    idB = int.Parse(strB.Substring(3));

                return idA.CompareTo(idB);
            });
    }

Pete
Author
28 Nov 2007 7:37 AM
Ole
Great thanks, Works very well!

However another problem showed up - if a bluetooth port is present
GetPortNames won't work because there is an error in the microsoft bluetooth
stack - I have posted that as a seperate question in this group.

Thanks
Ole

"Peter Duniho" <NpOeStPe***@NnOwSlPiAnMk.com> wrote in message
news:2007112610252050073-NpOeStPeAdM@NnOwSlPiAnMkcom...
Show quote
> On 2007-11-26 06:53:21 -0800, Brian Gideon <briangid***@yahoo.com> said:
>
>> Right your own IComparer and use one of the overloads of Array.Sort
>> that takes the IComparer as a parameter.
>
> As an alternative, you can use an anonymous method, passing it as a
> Comparison<T> to Array.Sort().
>
> Writing an IComparer would be the best option if it's something you'd need
> to use in multiple places.  I think an anonymous method is nicer if
> there's just the one place you need it, rather than creating a whole new
> class just for that purpose.
>
> It just depends on the OP's needs.
>
> As an example of both, for the OP's benefit:
>
>    class OrderNumeric : IComparer<string>
>    {
>        public int Compare(string strA, string strB)
>        {
>            int idA = int.Parse(strA.Substring(3)),
>                idB = int.Parse(strB.Substring(3));
>
>            return idA.CompareTo(idB);
>        }
>    }
>
>    void SortComPorts(string[] rgstrPorts)
>    {
>        Array.Sort<string>(rgstrPorts, new OrderNumeric());
>    }
>
>
>    void SortComPorts(string[] rgstrPorts)
>    {
>        Array.Sort<string>(rgstrPorts, delegate (string strA, string strB)
>            {
>                int idA = int.Parse(strA.Substring(3)),
>                    idB = int.Parse(strB.Substring(3));
>
>                return idA.CompareTo(idB);
>            });
>    }
>
> Pete
>

AddThis Social Bookmark Button