|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Array.Sort( comports )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 wrote:
> Hi, I have a class that implements that sorting, but I hope someone will > > 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: 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.
Show quote
On Nov 26, 3:13 am, "Ole" <o***@blabla.com> wrote: Right your own IComparer and use one of the overloads of Array.Sort> 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 that takes the IComparer as a parameter. 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's embarrassing. I meant to say "Write you own..."> that takes the IComparer as a parameter. 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 As an alternative, you can use an anonymous method, passing it as a > that takes the IComparer as a parameter. 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" <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 > |
|||||||||||||||||||||||