Sorting with String of Letters and numbers

rated by 0 users
This post has 6 Replies | 2 Followers

Top 100 Contributor
Posts 32
crad14 Posted: 10-18-2011 3:50 PM

I have a pretty big array and one of the columns I wanted to sort by is composed of Letters and Numbers....ie

Stuff25

Stuff15

when I do a sort of that column, it doesn't sort them in the correct order

 

I am pretty sure it is a pretty simple solution, but after some searching I can't seem to find anything helpful

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

can you show a few lines of the array - is it actually an array or are you workign from a CSV file?  Can you post an example of where the sorting breaks down?

Top 100 Contributor
Posts 32

It is both, I create an array from a csv.

 

Device

Device Port

DELL

Configuration

CISCO

Configuration

mercury

1

13.5-1

33

Cisco6

33

venus

2

13.5-3

22

Cisco7

22

earth

1

13.5-1

35

Cisco6

35

      

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

Assuming the column you are interested in sorting is

33

Cisco6

33

Cisco6

33

Cisco6

33Cisco6
22Cisco7
35Cisco6

How do you want it sorted?

The standard PowerShell sort produces this

PS> "33Cisco6", "22Cisco7", "35Cisco6" | sort
22Cisco7
33Cisco6
35Cisco6

Normally we would sort starting with the leftmost characters which produces this order.  What order do you need?

 

Top 100 Contributor
Posts 32

Sorry the table  I posted isn't very clear the 33 and Cisco6 are two diferent columns...

Regardless, I would like it to sort the cisco6 column. The way i would like it to sort would just be the way one might normally expect. 

So I would like all the Cisco6's to be together, followed by the Cisco7's then 8 etc...

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

If CiscoN where N is a number is one column then I would expect PowerShell to sort correctly e.g.

"Cisco6","Cisco7","Cisco6","Cisco6","Cisco8","Cisco6","Cisco8","Cisco6","Cisco7" | sort

What results are you getting?

One issue would be this

PS> "Cisco1","Cisco2","Cisco22","Cisco10" | sort
Cisco1
Cisco10
Cisco2
Cisco22

if you just use single digits for numbers less than 10 you will get these odd looking results. It is a function of the way sorts work.

 

 

Top 100 Contributor
Posts 32

Siddaway,

Of course now that I am trying this again, I am getting the results I want and I cannot duplicate the issue I was having before....Not sure what happened. 

 

Thanks for your help

Page 1 of 1 (7 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.