Page MenuHomeVyOS Platform

Implement intuitive node sorting
Closed, ResolvedPublic

Description

Names that follow ([a-z]+)([0-9]+) pattern are incredibly common in system administration. We have interfaces names eth10 or tun99, hosts names 0.pool.ntp.org and similar.
Standard lexicographic sorting gives counter-intuitive and unsatisfactory results for such strings: eth10 is considered less than eth9, the result is a total mess.

We need to implement a compare function that takes this into account such that:

  • eth10 > eth9
  • eth1.100 > eth1.99
  • www1.example.com > www0.example.com

Details

Difficulty level
Normal (likely a few hours)

Event Timeline

I like this kind of problem.

Probably just a matter of a natural sort order comparison function used with List.sort? Do you envision this belonging in a particular config directory, or rather as a general utility function we can use for natural sort ordering any strings at all?

Depending on your feelings toward batteries, we could either use BatString's numeric_compare or just crib the implementation of that function (with all due credit given, of course).

I've just done a quick test of the BatString.numeric_compare, looks perfect.

utop # List.sort BatString.numeric_compare ["eth9.77"; "eth10.33"; "eth0.1"] ;;
- : string list = ["eth0.1"; "eth9.77"; "eth10.33"]

In that case, BatString can just be added as a dependency, I guess, and we can call upon that function to sort as needed? Or would you still like a specific function for node sorting?

I think we can just reference it directly.

Since the Curly_renderer.render function needs to be called recursively, and if reference tree is given we need to only sort nodes for which keep_order is false, I think taking it as an optional argument is the best thing to do. Calls that do need to sort will take BatString.numeric_compare, and calls that don't will take (fun _ -> 0).

dmbaturin claimed this task.