PC Review


Reply
Thread Tools Rate Thread

Array.Sort( comports )

 
 
Ole
Guest
Posts: n/a
 
      26th Nov 2007
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


 
Reply With Quote
 
 
 
 
Lasse Vågsæther Karlsen
Guest
Posts: n/a
 
      26th Nov 2007
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
private.php?do=newpm&u=
http://presentationmode.blogspot.com/
 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      26th Nov 2007
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.
 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      26th Nov 2007
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..."
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      26th Nov 2007
On 2007-11-26 06:53:21 -0800, Brian Gideon <(E-Mail Removed)> 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

 
Reply With Quote
 
Ole
Guest
Posts: n/a
 
      28th Nov 2007
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" <(E-Mail Removed)> wrote in message
news:2007112610252050073-NpOeStPeAdM@NnOwSlPiAnMkcom...
> On 2007-11-26 06:53:21 -0800, Brian Gideon <(E-Mail Removed)> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Build a String Sort Key, sort a 2 dimen array Mar20 Neal Zimm Microsoft Excel Programming 0 20th Mar 2010 05:38 PM
Comports x dejotay Windows XP Help 3 17th Jan 2009 10:31 AM
NETCF - Array.Sort Method (Array, IComparer) - error aprivate Microsoft VB .NET 3 10th May 2005 02:16 PM
Maintain initial sort order with 2d array sort Bob Dankert Microsoft C# .NET 3 11th May 2004 02:32 AM
No Comports Jessica Parra Microsoft Windows 2000 0 10th May 2004 11:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:38 PM.