Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Powershell sort-object

Posted on 2011-10-31
4
Medium Priority
?
915 Views
Last Modified: 2012-05-12
Hi there,

is it possible to use the sort-object cmdlet to sort strings depending on their length? E.g. I have these values in an Array:


111
21
1111
3

I should sort them like this:

3
21
111
1111

How to achieve that?

Cheerio


0
Comment
Question by:ptea
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 37057084
Try this

$Arr = "111", "21", "1111", "3"
$Arr | Sort-Object length
0
 

Author Comment

by:ptea
ID: 37057459
thx for your response - my description was not detailed enough, in fact the input file  (csv) looks like this:

a,b,c
111,bababa,skjskssk
11,snakj,skjasiohio
1234,cjdsh,nsuidn

I should sort the values depending on the values in the column a by the length of the string:

11,snakj,skjasiohio
111,bababa,skjskssk
1234,cjdsh,nsuidn

Cheers
0
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 total points
ID: 37058174
ok, that makes it a little more difficult. Given your example you could try something liek this.


Import-Csv C:\test.csv | Select a, b, c, @{n='Length';e={ ($_.a).length }} | Sort length
0
 

Author Closing Comment

by:ptea
ID: 37062019
thx
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question