1
00:00:01,040 --> 00:00:01,530
Next,

2
00:00:01,530 --> 00:00:09,120
let's look at sorting. Once again, let's start with Get‑Vegetable and pipe this

3
00:00:09,120 --> 00:00:14,740
to sort‑object, and I'm going to sort on the Name property.

4
00:00:14,740 --> 00:00:16,680
And there you can see that everything is now

5
00:00:16,680 --> 00:00:19,540
alphabetized based on that Name property.

6
00:00:19,540 --> 00:00:21,340
If I wanted to,

7
00:00:21,340 --> 00:00:26,410
I could also sort in the Name property, and this time I'll do ‑descending to

8
00:00:26,410 --> 00:00:29,940
show you that there is a different way to sort if you need to.

9
00:00:29,940 --> 00:00:34,140
Here's where it gets a little challenging when it comes to sorting.

10
00:00:34,140 --> 00:00:37,720
Let's go back here and do Get‑Vegetable, and say I want to sort on

11
00:00:37,720 --> 00:00:40,000
that state property that you see listed there.

12
00:00:40,000 --> 00:00:46,040
It shows me whether it's raw, roasted, grilled, steamed, or whatever.

13
00:00:46,040 --> 00:00:51,590
So I sorted, but you know, that doesn't look like it's sorted or did anything.

14
00:00:51,590 --> 00:00:52,850
So what's going on here?

15
00:00:52,850 --> 00:00:57,040
And this is where, again, Get‑Member is your friend.

16
00:00:57,040 --> 00:01:00,240
So I'm going to pipe Get‑Vegetable again to Get‑Member.

17
00:01:00,240 --> 00:01:04,700
And if we look at that state property, well,

18
00:01:04,700 --> 00:01:06,370
actually there is no state property.

19
00:01:06,370 --> 00:01:09,040
There's something there called CookedState,

20
00:01:09,040 --> 00:01:12,770
and you can see that the type of that property is

21
00:01:12,770 --> 00:01:18,040
something called a PSTeachingTools.VegStatus.

22
00:01:18,040 --> 00:01:22,110
Now this is where we get a little advanced here, so bear with

23
00:01:22,110 --> 00:01:24,980
me. This is not something that you'll need to do all the time,

24
00:01:24,980 --> 00:01:28,950
but I want you to at least kind of understand why PowerShell is

25
00:01:28,950 --> 00:01:32,040
sorting the way that it is.

26
00:01:32,040 --> 00:01:37,290
So let's do Get‑Vegetable and sort on that CookedState

27
00:01:37,290 --> 00:01:41,540
property, now that we know the actual name.

28
00:01:41,540 --> 00:01:43,670
All right, so that's a little bit better,

29
00:01:43,670 --> 00:01:44,740
although, you know,

30
00:01:44,740 --> 00:01:47,730
it still doesn't look fully sorted. What you have

31
00:01:47,730 --> 00:01:51,700
to understand, and this, again, it's kind of an advanced topic,

32
00:01:51,700 --> 00:01:54,340
not something you'll have to deal with all the time, but

33
00:01:54,340 --> 00:01:58,540
understand that some property names have a value that is

34
00:01:58,540 --> 00:02:00,900
referred to as an enumeration.

35
00:02:00,900 --> 00:02:01,560
In other words,

36
00:02:01,560 --> 00:02:07,740
it's an assigned value that corresponds to some underlying numeric value.

37
00:02:07,740 --> 00:02:11,730
We can use the .NET Framework to look at those things.

38
00:02:11,730 --> 00:02:16,440
So remember that type name that I showed you before, PSTeachingTools.VegStatus,

39
00:02:16,440 --> 00:02:21,740
I'm going to take a look to see if that is an enumeration,

40
00:02:21,740 --> 00:02:25,840
and we're going to do that like this.

41
00:02:25,840 --> 00:02:29,090
So I'm going to get that get‑member cookedState. All right,

42
00:02:29,090 --> 00:02:34,570
so I can see the property, and again, I can verify the property type of

43
00:02:34,570 --> 00:02:36,910
that property or the object type of that property,

44
00:02:36,910 --> 00:02:37,600
I should say,

45
00:02:37,600 --> 00:02:42,790
is that VegStatus. Now, I don't know really what that is, so I'm going to test

46
00:02:42,790 --> 00:02:49,650
that. I'm going to use some .NET code here that's going to get all the names

47
00:02:49,650 --> 00:02:54,740
that might be related to that particular class.

48
00:02:54,740 --> 00:02:55,680
And there's the list.

49
00:02:55,680 --> 00:02:58,380
And that list should look familiar because it's the list

50
00:02:58,380 --> 00:03:03,720
that we saw in the output. However, what it's really sorting on,

51
00:03:03,720 --> 00:03:07,940
though, is the underlying numeric value,

52
00:03:07,940 --> 00:03:10,700
and this is another little trick that you can use to get that.

53
00:03:10,700 --> 00:03:16,140
So I'm going to get the values for that enumeration

54
00:03:16,140 --> 00:03:18,870
and then just select the value,

55
00:03:18,870 --> 00:03:25,290
the underlying numeric value. And that property name is Value__. So

56
00:03:25,290 --> 00:03:30,040
raw has a value of 0, boiled is 1, and so on.

57
00:03:30,040 --> 00:03:31,950
So when I'm doing my sort,

58
00:03:31,950 --> 00:03:36,950
it's actually sorting on that numeric value and not the

59
00:03:36,950 --> 00:03:40,380
alphabetic name that you see in the enumeration.

60
00:03:40,380 --> 00:03:43,840
And this can be a little tricky and be a little confusing

61
00:03:43,840 --> 00:03:47,240
as to why things aren't working the way you expect them to.

62
00:03:47,240 --> 00:03:50,640
Here is another good example.

63
00:03:50,640 --> 00:03:56,540
If I do get‑service and sort on the status property in descending order,

64
00:03:56,540 --> 00:03:57,000
all right,

65
00:03:57,000 --> 00:04:01,580
so you can see stopped running, again, you would think that if it's in

66
00:04:01,580 --> 00:04:07,840
descending, I should see Running at the end and not Stopped.

67
00:04:07,840 --> 00:04:11,640
So let's follow the same concept that we looked at with vegetables.

68
00:04:11,640 --> 00:04:13,460
So I do get‑service bits.

69
00:04:13,460 --> 00:04:16,340
We'll just get a representative service,

70
00:04:16,340 --> 00:04:19,520
pipe it to get‑member, and look at that status property.

71
00:04:19,520 --> 00:04:26,940
There we can see a type name of System.ServiceProcess.ServiceControllerStatus.

72
00:04:26,940 --> 00:04:34,640
Let's check to see if that is an enumeration. So we'll try getting names on it.

73
00:04:34,640 --> 00:04:36,510
And sure enough, we see some names.

74
00:04:36,510 --> 00:04:39,820
So now that I know it's an enumeration,

75
00:04:39,820 --> 00:04:44,530
I know it is probably sorting on the underlying values

76
00:04:44,530 --> 00:04:47,340
for the enumeration. And in this case,

77
00:04:47,340 --> 00:04:51,890
Stopped is being sorted first and Running comes down later in the list.

78
00:04:51,890 --> 00:04:55,660
So if I really wanted for the services anyway to be alphabetical,

79
00:04:55,660 --> 00:05:00,760
I would want to sort them in ascending order in

80
00:05:00,760 --> 00:05:04,040
order to see them alphabetically.

81
00:05:04,040 --> 00:05:07,170
But that's the kind of thing that can be a little

82
00:05:07,170 --> 00:05:10,110
confusing and kind of bang your head and say,

83
00:05:10,110 --> 00:05:11,830
why is PowerShell behaving this way?

84
00:05:11,830 --> 00:05:13,460
Don't try to remember. This is, again,

85
00:05:13,460 --> 00:05:16,970
not something you're going to need to do all the time, but if it comes up,

86
00:05:16,970 --> 00:05:18,780
you can go, oh, wait a minute, Jeff showed me this thing about

87
00:05:18,780 --> 00:05:21,780
enumeration, and then you can go back and look at it.

88
00:05:21,780 --> 00:05:24,380
You can also do multiple sorts.

89
00:05:24,380 --> 00:05:28,650
So I do Get‑Process and get all the processes that start with s,

90
00:05:28,650 --> 00:05:33,840
you can actually sort, in this case, name and id.

91
00:05:33,840 --> 00:05:36,490
So if I have multiple processes, like here,

92
00:05:36,490 --> 00:05:39,140
like you see service host,

93
00:05:39,140 --> 00:05:43,440
the sort will go first on the name, and then if there is a duplicate,

94
00:05:43,440 --> 00:05:46,280
it will then sort on the process id,

95
00:05:46,280 --> 00:05:52,350
which is this column here. So you can sort on multiple properties,

96
00:05:52,350 --> 00:05:56,310
but the sort order is always going to be the same, either ascending or

97
00:05:56,310 --> 00:06:04,000
descending. You can't sort one property ascending and then the second property descending; that won't work.

