Sorting a table of IPs

Hi,

currently I’m struggling with sorting a table by IPs in a query/template.

Obviuously they are sorted as text, which seems to be designed :slight_smile:

However, is there a trick to sort them by octett?

Example:
Current sorted output:
10.1.1.1
10.1.1.20
10.1.1.21
10.1.1.3
10.1.2.40
10.1.2.5

What I would want:
10.1.1.1
10.1.1.3
10.1.1.20
10.1.1.21
10.1.2.5
10.1.2.40
etc.

Anyone already had a similar requirement already and solved it? Maybe there is a way with RegEx or something?

Thanks a lot!

What you can do is define a simple space script function and use that for sorting (I asked ChatGPT to help with this, so I’m sure there’s a better way, this seems to work though):

```space-script
silverbullet.registerFunction({name: "sortableIp"}, ip => {
  return ip.split('.')
    .map(octet => ('000' + octet).slice(-3)) // Pad each octet with leading zeros and take the last three digits
    .join('.');
});
```

Then in your query you can use order by sortableIp(ip)

1 Like

Wow, thank you very much!
Works perfectly!

Note to myself: Get used to work with chatGPT, too :slight_smile:

From day to day Silverbullet gets even more useful for me :slight_smile:

1 Like