Next,
let's look at sorting. Once again, let's start with Get‑Vegetable and pipe this
to sort‑object, and I'm going to sort on the Name property.
And there you can see that everything is now
alphabetized based on that Name property.
If I wanted to,
I could also sort in the Name property, and this time I'll do ‑descending to
show you that there is a different way to sort if you need to.
Here's where it gets a little challenging when it comes to sorting.
Let's go back here and do Get‑Vegetable, and say I want to sort on
that state property that you see listed there.
It shows me whether it's raw, roasted, grilled, steamed, or whatever.
So I sorted, but you know, that doesn't look like it's sorted or did anything.
So what's going on here?
And this is where, again, Get‑Member is your friend.
So I'm going to pipe Get‑Vegetable again to Get‑Member.
And if we look at that state property, well,
actually there is no state property.
There's something there called CookedState,
and you can see that the type of that property is
something called a PSTeachingTools.VegStatus.
Now this is where we get a little advanced here, so bear with
me. This is not something that you'll need to do all the time,
but I want you to at least kind of understand why PowerShell is
sorting the way that it is.
So let's do Get‑Vegetable and sort on that CookedState
property, now that we know the actual name.
All right, so that's a little bit better,
although, you know,
it still doesn't look fully sorted. What you have
to understand, and this, again, it's kind of an advanced topic,
not something you'll have to deal with all the time, but
understand that some property names have a value that is
referred to as an enumeration.
In other words,
it's an assigned value that corresponds to some underlying numeric value.
We can use the .NET Framework to look at those things.
So remember that type name that I showed you before, PSTeachingTools.VegStatus,
I'm going to take a look to see if that is an enumeration,
and we're going to do that like this.
So I'm going to get that get‑member cookedState. All right,
so I can see the property, and again, I can verify the property type of
that property or the object type of that property,
I should say,
is that VegStatus. Now, I don't know really what that is, so I'm going to test
that. I'm going to use some .NET code here that's going to get all the names
that might be related to that particular class.
And there's the list.
And that list should look familiar because it's the list
that we saw in the output. However, what it's really sorting on,
though, is the underlying numeric value,
and this is another little trick that you can use to get that.
So I'm going to get the values for that enumeration
and then just select the value,
the underlying numeric value. And that property name is Value__. So
raw has a value of 0, boiled is 1, and so on.
So when I'm doing my sort,
it's actually sorting on that numeric value and not the
alphabetic name that you see in the enumeration.
And this can be a little tricky and be a little confusing
as to why things aren't working the way you expect them to.
Here is another good example.
If I do get‑service and sort on the status property in descending order,
all right,
so you can see stopped running, again, you would think that if it's in
descending, I should see Running at the end and not Stopped.
So let's follow the same concept that we looked at with vegetables.
So I do get‑service bits.
We'll just get a representative service,
pipe it to get‑member, and look at that status property.
There we can see a type name of System.ServiceProcess.ServiceControllerStatus.
Let's check to see if that is an enumeration. So we'll try getting names on it.
And sure enough, we see some names.
So now that I know it's an enumeration,
I know it is probably sorting on the underlying values
for the enumeration. And in this case,
Stopped is being sorted first and Running comes down later in the list.
So if I really wanted for the services anyway to be alphabetical,
I would want to sort them in ascending order in
order to see them alphabetically.
But that's the kind of thing that can be a little
confusing and kind of bang your head and say,
why is PowerShell behaving this way?
Don't try to remember. This is, again,
not something you're going to need to do all the time, but if it comes up,
you can go, oh, wait a minute, Jeff showed me this thing about
enumeration, and then you can go back and look at it.
You can also do multiple sorts.
So I do Get‑Process and get all the processes that start with s,
you can actually sort, in this case, name and id.
So if I have multiple processes, like here,
like you see service host,
the sort will go first on the name, and then if there is a duplicate,
it will then sort on the process id,
which is this column here. So you can sort on multiple properties,
but the sort order is always going to be the same, either ascending or
descending. You can't sort one property ascending and then the second property descending; that won't work.