How to sort Scala collections classes (sortWith, sorted, Ordered, Ordering)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.28, “How to Sort a Scala Collection”


You want to sort a sequential collection. Or, you want to implement the Ordered trait in a custom class so you can use the sorted method, or operators like <, <=, >, and >= to compare instances of your class.

Scala List class examples: range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll share examples of the most common List operations (methods).

Show unique list elements with the Linux sort -u command

If you're ever working on a Unix or Linux system, and have a list with duplicated items in it, and want a smaller list of only the unique items in the list, the sort command is your friend.

I just ran into a situation where I generated a list of fonts on a Mac OS X (Unix) system, and my list ended up with a bunch of duplicated names, like this:

Perl array/sort FAQ - Perl integer array sorting

Summary: How to sort Perl arrays, in this case, a Perl integer array.

Sorting a Perl integer array (technically a Perl numeric array) is relatively simple, well, at least once you know the magic formula. The key thing to know is that you need to provide the Perl sort function a helper function (or block of code) that tells it how to sort integers. By default, the Perl sort function sorts arrays in ASCII order, and that's not going to work very well for you.

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 ‘ps’ command output

Linux ps sort FAQ: Can you share some examples of how to sort the ps command?

Sure. In this article we'll take a look at how to sort the Linux ps command output -- without using the Linux sort command.

Before we get started, the important thing to know is that the Linux ps command supports a --sort argument, and that argument takes a number of key values, and those keys indicate how you want to support the ps output.

Here's a quick look at the --sort information from the ps command man page:

Java array sorting: How to sort a Java String array

Java array FAQ: Can you share an example of how to sort a Java String array?

Sure. The following source code shows how to sort an array of strings. In short, you can easily sort a string array with the Arrays class sort method, shown in bold below:

How to sort the Linux 'ps' command output by RAM

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: