Array.Sort( comports )

  • Thread starter Thread starter Ole
  • Start date Start date
O

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
 
Ole said:
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.
 
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.
 
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..."
 
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
 
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 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
 
Back
Top