sort

How to sort Linux ls command file output

A couple of days ago I was asked how to sort the output from the Unix and Linux ls command. Off the top of my head I knew how to sort the ls output by file modification time, and also knew how to sort ls with the Linux sort command, but I didn't realize there were other cool file sorting options available until I looked them up.

In this short tutorial I'll demonstrate the Unix/Linux ls command file sorting options I just learned.

Linux process memory usage - how to sort the ps command

A lot of times when we need to look at Unix or Linux process information (like memory or cpu usage) we just issue some variation of the Linux ps command, like this:

ps auxww

or this:

Java array sort example - how to sort a Java String array

Here's the source code for a complete class that demonstrates how to sort a Java String array:

A cool way to sort the Linux ps command by memory use

With this site hosted on a virtual server, I'm fighting quite a battle over memory use with the new LAMP architecture in place. As I try to learn more about which applications are using the most memory, I ran into this cool ps command last night that sorts the ps output by memory use, specifically by the rss field:

ps aux --sort:rss

Here's the important output from that command:

Perl hash sort - How to sort a Perl hash by the hash key

Question: How do I sort a Perl hash by the hash key?

Answer: Sorting the output of a Perl hash by the hash key is fairly straightforward. It involves two Perl functions, keys and sort, along with the good old foreach statement.

This is easiest to demonstrate by example. Suppose we have a class of five students. Their names are kim, al, rocky, chrisy, and jane.

Perl hash sort - How to sort a Perl hash by the hash value

Question: How do I sort a Perl hash by the hash value?

First ... sorting a Perl hash by the hash key

If you need to sort a Perl hash by the hash key, this is a pretty well-known recipe. It's covered in another Q&A article titled "How to sort a hash by the hash key".

Linux sort command examples

Introduction

As its name implies, the Unix/Linux sort command lets you sort text information. This article shares several examples of the sort command.

Sorting output of the ls command

You can use the Linux sort command to sort all kinds of output from other commands. For instance, here's an example where I sort the output of the ls -al command:

How to sort an array of Ruby objects by multiple class fields

In a previous tutorial I wrote about how to sort an array of Ruby objects by one field in the object. In today's tutorial I'd like to demonstrate how to sort an array of Ruby objects by multiple attributes (or fields) of the class contained by the array.

How to sort an array of objects by one field

Sorting an array of objects by one column in the object (class) is pretty simple with Ruby. Here's a quick demo of how I just did this when working on sorting the rows in a CSV file in a simple Ruby script.

How to open and sort a CSV file with Ruby

Here's the complete source code for a simple Ruby script which performs the following tasks:

Syndicate content