PowerShell 中进行列表展示的排序-倒序

Order Your Output by Easily Sorting Objects in PowerShell

★★★★★

★★★★

★★★

★★

January 10, 2012 by The Scripting Guys // 9 Comments

Summary: Much of the time, there is no guarantee to the order in which Windows PowerShell returns objects. This blog explains how to fix that issue.

Hey, Scripting Guy! I have a problem with Windows PowerShell. It seems that it deliberately randomizes the output. I mean, there seems to be no rhyme or reason to the way that information is returned from a cmdlet. Am I alone in this frustration, or is there some secret sauce that I am missing? Windows PowerShell is cool, but if I always have to put data into an Excel spreadsheet just to sort it, then it is not much better than VBScript in my mind. Help me, oh fount of Windows PowerShell wisdom.

—CD

Hello CD,

Microsoft Scripting Guy, Ed Wilson, is here. The other day, the Scripting Wife and I were at the first ever Windows PowerShell User Group meeting in Charlotte, North Carolina. It was really cool. We love being able to interact with people who love Windows PowerShell as much as we do. Next month, we are having a script-club type of meeting; we encourage people to show up with the Windows PowerShell scripts they are working on, so it will be a show-and-tell type of meeting.

Use Sort-Object to organize output

Anyway, after the user group meeting, when we were all standing around, one of the attendees came up to me and asked me in what order Windows PowerShell returns information. The answer is that there is no guarantee of return order in most cases. The secret sauce is to use the built-in sorting mechanism from Windows PowerShell itself. In the image that follows, the results from the Get-Process cmdlet appear to sort on the ProcessName property.

One could make a good argument that the processes should sort on the process ID (PID) or on the amount of CPU time consumed, or on the amount of memory utilized. In fact, it is entirely possible that for each property supplied by the Process object, someone has a good argument for sorting on that particular property. Luckily, custom sorting is easy to accomplish in Windows PowerShell. To sort returned objects in Windows PowerShell, pipe the output from one cmdlet to the Sort-Object cmdlet. This technique is shown here where the Sort-Object cmdlet sorts the Process objects that are returned by the Get-Process cmdlet.

Get-Process | Sort-Object id

The command to sort the Process objects on the ID property and the output associated with that command are shown in the image that follows.

Reversing the sort order

By default, the Sort-Object cmdlet performs an ascending sort—the numbers range from small to large. To perform a descending sort requires utilizing the Descending switch.

Note:  There is no Ascending switch for the Sort-Object cmdlet because that is the default behavior.

To arrange the output from the Get-Process cmdlet such that the Process objects appear from largest process ID to the smallest (the smallest PID is always 0—the Idle process), choose the ID property to sort on, and use the Descending switch as shown here:

Get-Process | Sort-Object id –Descending

The command to perform a descending sort of processes based on the process ID, and the output associated with that command are shown in the image that follows.

When you use the Sort-Object cmdlet to sort output, keep in mind that the first position argument is the property or properties upon which to sort. Because Property is the default means that using the name Property in the command is optional. Therefore, the following commands are equivalent:

Get-Process | Sort-Object id –Descending

Get-Process | Sort-Object -property id –Descending

In addition to using the default first position for the Property argument, the Sort-Object cmdlet is aliased by sort. By using gps as an alias for the Get-Process cmdlet, sort as an alias for Sort-Object, and a partial parameter of des for Descending, the syntax of the command is very short. This short version of the command is shown here.

gps | sort id –des

Sorting multiple properties at once

The Property parameter of the Sort-Object cmdlet accepts an array (more than one) of properties upon which to sort. This means that I can sort on the process name, and then sort on the working set of memory that is utilized by each process (for example). When supplying multiple property names, the first property sorts, then the second property sorts.

The resulting output may not always meet expectations, and therefore, may require a bit of experimentation. For example, the command that follows sorts the process names in a descending order. When that sort completes, the command does an additional sort on the WorkingSet(ws is the alias) property. However, this second sort is only useful when there happen to be multiple processes with the same name (such as the svchost process). The command that is shown here is an example of sorting on multiple properties.

Get-Process | Sort-Object -Property name, ws –Descending

The figure that is shown here illustrates the output from the command to sort Process objects based on name and ws properties.

When the name and ws properties reverse order in the command, the resulting output is not very useful because the only sorting of thename property happens when multiple processes have an identical working set of memory. The command that is shown here reverses the order of the WorkingSet and the process name properties.

Get-Process | Sort-Object -Property ws, name –Descending

The output that is shown here shows that there is very little grouping of process names. In this example, adding the name property does not add much value to the command.

Sorting and returning unique items

At times, I might want to see how many different processes are running on a system. To do this, I can filter duplicate process names by using the Unique switch. To count the number of unique processes that are running on a system, I pipe the results from the Sort-Object cmdlet to the Measure-Object cmdlet. This command is shown here.

Get-Process | Sort-Object -Property name -Descending -Unique | measure-object

To obtain a baseline that enables me to determine the number of duplicate processes, I drop the Unique switch. This command is shown here.

Get-Process | Sort-Object -Property name -Descending | measure-object

Performing a case sensitive sort

One last thing to discuss when sorting items is the CaseSensitive switch. When used, the CaseSensitive switch sorts lowercase letters first, then uppercase. The following commands illustrate this.

$a = “Alpha”,”alpha”,”bravo”,”Bravo”,”Charlie”,”charlie”,”delta”,”Delta”

$a | Sort-Object –CaseSensitive

When the two previous commands run, the output places the lowercase version of the word prior to the uppercase version. This output appears in the figure that follows.

CD, that is all there is to sorting with Windows PowerShell. Pipeline Week will continue tomorrow when I will talk about grouping things with Windows PowerShell.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at [email protected], or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

时间: 2024-07-30 22:00:12

PowerShell 中进行列表展示的排序-倒序的相关文章

快速数据列表展示(编辑中)

在页面数据和控件的自动交换机制中,我们通过PageX实现了一种快速的控件和数据交换机制,能够方便快捷地完成数据库中的数据记录的管理.一般情况下,在进入这个编辑页面之前,还应该有另外一个页面,可以一次展示多条数据记录的信息.在这个数据的列表界面中,可以进行各种操作,如删除.跳转.编辑等.这个数据列表页面不仅可以按照数据库的分类展示数据,也可以根据需要进行展示,如按指定字段排序.根据查询结果展示.分页展示等. 用什么来展示数据列表? 由于数据列表页面在数据管理中十分常用,因此,在微软的开发工具的发展

2019年6月12日——开始记录并分享学习心得——Python3.7中对列表进行排序

Python中对列表的排序按照是排序是否可以恢复分为:永久性排序和临时排序. Python中对列表的排序可以按照使用函数的不同可以分为:sort( ), sorted( ), reverse( ). 下边具体介绍一下这三种函数的使用: 1. 使用sort( )对列表进行永久性排序 my_love = ['sleep', 'weekend', 'games', 'learning', 'travel'] # 定义一个列表my_love.sort() # 对列表按照字母顺序永久性排序print(my

ListView列表拖拽排序

ListView列表拖拽排序可以参考Android源代码下的Music播放列表,他是可以拖拽的,源码在[packages/apps/Music下的TouchInterceptor.java下]. 首先是搭建框架,此处的ListView列表类似于QQ消息列表,当然数据只是模拟,为了简单起见,没有把ListView的条目的所有的属性全部写上.首先是消息的实体类Msg.java: package me.chenfuduo.mymsgdrag; public class Msg { private in

列表展示测试

列表展示测试1 列表页面显示: 1.   确认页面的默认排序方式,字段+升降续: 2.   含link的列,验证其有效性,即,点击后的跳转是否正确: 3.   第一列的选择框,“全选”和“部分 选择”需有效:部分选中时,全选按钮应自动取消. 顶部搜索功能: 4.   逐个测试每个搜索条件的有效性: 5.   做2-3个组合条件的查询,验证结果:合计共有N+3个搜索条件的测试. 6.   有时间区间的,验证列表项的开始到结束时间 和 选择区间有交叉,则为有效,且包含所选日期的记录: 7.   条件

(18)Powershell中的字符串拆分运算符

Powershell中提供了对字符串的拆分操作运算符.-split 运算符将一个字符串拆分成多个字符串. 拆分运算符 拆分运算符用于将一个或多个字符串拆分为多个子字符串.可更改拆分操作的以下元素: (1)定界符.默认为空白,但是可指定字符.字符串.模式或用于设置定界符的脚本块. (2)子字符串的最大数目.默认设置为返回所有子字符串.如果指定的数字小于子字符串数,则其余子字符串将合并到最后一个子字符串中. (3)用于指定定界符匹配条件的选项,如 SimpleMatch 和 Multiline. 拆

C++ STL中Map的按Key排序和按Value排序

原文  http://blog.csdn.net/iicy266/article/details/11906189 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key:该学生的成绩用int类型,作为value.这样一来,我们可以根据学

Python学习笔记整理(五)Python中的列表.

列表和字段,这两种类型几乎是Python所有脚本的主要工作组件.他们都可以在原处进行修改,可以按需求增加或缩短,而且包含任何种类的对象或者被嵌套. 一.列表 列表的主要属性: *任意对象的有序集合 从功能上看,列表就是收集其他对象的地方,可以把它看作组.列表所包含每一项都保持了从左到右的位置顺序(它们是序列) *通过偏移读取 和字符串一样,可以通过列表对象的偏移对其进行索引,从而读取对象的某一部分内容.可以自行分片和合并之类的任务. *可变长度,异构以及任意嵌套 列表可以实地增长或者缩短,并且可

对NSArray中自定义的对象进行排序

本文译自How to sort NSArray with custom objects. 我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天程序则又一个Friend类,点菜程序会有一个Recipe类等.有时候我们希望在程序中显示的列表数据是按照一定顺序进行排列的,本文我们就来看看在iOS中有哪些方法可以对NSArray中的对象进行排序.下面是目录: 小引 使用NSComparator进行排序 使用NSDescriptor进行

[android] 手机卫士黑名单功能(列表展示)

先把要拦截的电话号码保存到数据库中,拦截模式用个字段区分,1 电话拦截,2 短信拦截,3全部拦截 新建Activity类CallSmsSafeActivity.java 新建布局文件activity_call_sms_safe.xml 列表展示所有的黑名单手机号码 在布局文件中添加<ListView>控件,定义一个id 获取ListView对象 调用ListView对象的setAdapter()方法,参数:ListAdapter对象 定义内部类CallSmsSafeAdapter继承系统的Ba