sort

Kotlin collections methods: examples and syntax

As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.

Sample data

First, here’s some sample data:

val a = listOf(10, 20, 30, 40, 10)
val names = listOf("joel", "ed", "chris", "maurice")

Kotlin sortedWith syntax and lambda examples

As a quick note, here’s an example of how to use the Kotlin sortedWith syntax with an anonymous function (lambda). Given this list of integers:

val list = listOf(7,3,5,9,1,3)

Here’s an example of how to use sortedWith using a Comparator and lambda:

Kotlin sortedBy syntax and examples

As a quick note, here’s an example of the Kotlin sortedBy syntax. Given this list of strings:

val names = listOf("kim", "julia", "jim", "hala")

the comments after these examples show how the Kotlin sortedBy function works:

How to show the largest files under a directory on Mac OS X (Unix)

Here’s an example that shows how to find the largest files under a directory on MacOS and Linux/Unix systems.

A du/sort command to show the largest files under a directory on Mac OS X

The Unix/Linux command that worked for me on my MacOS system is this:

$ du -a * | sort -r -n | head -10

du is the disk usage command, and the -a flag says, “Display an entry for each file in a file hierarchy.” Then I use the sort command to sort the du output numerically and in reverse. After that, head -10 shows only the first ten lines of output. In the Music folder on my Mac the command and output look like this:

Methods on the Scala collections classes, organized by category

When I wrote the Scala Cookbook, I gave each recipe and then each chapter my full attention. I thought that if I wrote each recipe as well as possible, and included important recipes in each chapter, well, I wanted each chapter to be worth the price of the entire book. That was my goal.

As a result of this effort -- and perhaps to the chagrin of my editor -- the Scala collections chapters ended up being 130 pages in length.

Sorting ‘ls’ command output by filesize

I just noticed that some of the MySQL files on this website had grown very large, so I wanted to be able to list all of the files in the MySQL data directory and sort them by filesize, with the largest files shown at the end of the listing. This ls command did the trick, resulting in the output shown in the image:

ls -Slhr

The -S option is the key, telling the ls command to sort the file listing by size. The -h option tells ls to make the output human readable, and -r tells it to reverse the output, so in this case the largest files are shown at the end of the output.

How to use sortable Sets in Scala (SortedSet, TreeSet, LinkedHashSet)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.28, “How to Use Sortable Sets in Scala”

Problem

You want to be able to store and retrieve items from a set in a sorted order.

Solution

To retrieve values from a set in sorted order, use a SortedSet. To retrieve elements from a set in the order in which elements were inserted, use a LinkedHashSet.

A SortedSet returns elements in a sorted order:

How to sort a Scala Map by key or value (sortBy, sortWith)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.23, “How to Sort an Existing Map by Key or Value”

Problem

You have an unsorted map and want to sort the elements in the map by the key or value.

Solution

Given a basic, immutable Map:

How to sort Arrays in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes in the book, Recipe 11.10, “How to Sort Arrays in Scala”

Problem

You want to sort the elements in an Array (or ArrayBuffer).