Erlang - Sort list of files according to postfix number before extension

I have a list:

["storage.pages/4532.1.html",
 "storage.pages/4532.12.html",
 "storage.pages/4532.2.html",
 "storage.pages/4532.3.html",
 "storage.pages/4532.4.html",
 "storage.pages/4532.5.html"]

And I want to do something like this:

last_filename(PageId)->
  Files1 = filelib:wildcard("storage.pages/" ++ wf:to_list(PageId) ++ ".*.html"),
  hd(lists:reverse(lists:sort(Files1))).

which at this moment returns "storage.pages/4532.5.html" but I want it to return "storage.pages/4532.12.html".

What are good adjustments I need to make to my function so that it always returns the element in the list that has the highest postfix number in the basename?

Cheers!

For this case you don't need convert part of string to number. Just first do comparisons length of strings.

L=["storage.pages/4532.1.html",
 "storage.pages/4532.12.html",
 "storage.pages/4532.2.html",
 "storage.pages/4532.3.html",
 "storage.pages/4532.4.html",
 "storage.pages/4532.5.html"].
RComp = fun(X,Y)->length(X) > length(Y) orelse X > Y end.
hd(lists:sort(RComp,L)).

output:

"storage.pages/4532.12.html"

Erlang -- file_sorter, Chunks containing binary terms are read from a sequence of files, sorted HeaderLength determines the number of bytes preceding each binary and Fun​(A, B) is to return true if A comes before B in the ordering, otherwise false. A function that reads chunks from the disk log and returns a list of binaries is used as input. By default about 512*1024 bytes read from files are sorted internally. This option is rarely needed. {no_files, NoFiles} By default 16 files are merged at a time. This option is rarely needed. As an alternative to sorting files, a function of one argument can be specified as input.

Efficient solution (in Perl world well known as Schwartzian transform):

L = ["storage.pages/4532.1.html",
 "storage.pages/4532.12.html",
 "storage.pages/4532.2.html",
 "storage.pages/4532.3.html",
 "storage.pages/4532.4.html",
 "storage.pages/4532.5.html"].
element(2, lists:max([ {list_to_integer(lists:last(string:tokens(filename:basename(X, ".html"), "."))), X} || X <- L])).

As a function in module

last_filename(PageId) ->
    Files = filelib:wildcard("storage.pages/" ++ wf:to_list(PageId) ++ ".*.html"),
    FilesWVersion = [ {version(FN), FN} || FN <- Files ],
    {_, LF} = lists:max(FilesWVersion),
    LF.

version(FileName) ->
    BN = filename:basename(X, ".html"),
    VS = lists:last(string:tokens(BN, ".")),
    list_to_integer(VS).

Questions for tag erlang, Erlang - Sort list of files according to postfix number before extension · Understanding Elixir badarg error message · Cannot connect to rabbitmq management  Combines the elements of three lists of equal length into one list. For each triple X, Y, Z of list elements from the three lists, the element in the result list is Combine(X, Y, Z). zipwith3(fun(X, Y, Z) -> {X,Y,Z} end, List1, List2, List3) is equivalent to zip3(List1, List2, List3). Examples:

I solved it like this but suggestions for improvements are welcome:

last_filename(Page)->
  Files1 = filelib:wildcard("storage.pages/" ++ wf:to_list(Page#page.id) ++ ".*.html"),
  Files2 = lists:sort(fun(X, Y) ->
    X1 = wf:to_integer(lists:nth(2, lists:reverse(string:split(X, ".", all)))),
    Y1 = wf:to_integer(lists:nth(2, lists:reverse(string:split(Y, ".", all)))),
    X1 > Y1
  end, Files1),
  hd(Files2).

The Erlang Runtime System, In Elixir you use the function Node.list with the argument :this , and in Erlang Erlang is compiled from source code modules in .erl files to fat binary .beam files. you find code that generates atom with a number followed by a postfix name, In order to implement this in C, BEAM uses the GCC extension "labels as values". If you want it to use as litlle CPU as possible then you probably want to do the sorting using lists:keysort(2, List) after you have received all info. If you want to deliver the sorted list as fast as possible when the last message is received (and there is CPU time available to do some preemptive sorting) then this type of insertion sort might be faster.

Filters, For the first two files in the list, the default mode will be determined by the As of Ansible version 2.9, it's also possible to initialize the random number an unsorted list of VLAN integers into a sorted string list of integers according to Erlang ( %. Prior to ansible 2.0, if “regex_replace” filter was used with variables inside  1 Erlang - Sort list of files according to postfix number before extension Jul 11 '18 0 Unmarshal unknown JSON fields without structs Jul 6 '19 0 How make expanded and stretched layout box with Fyne Mar 6

Functionally Solving Problems, In order to use it we will need to feed it two operands, which will be taken from showing the operands 3 and 4 taken from the stack, used in the postfix Open a file named calc.erl . You might have noticed that Erlang's lists act a lot like a stack. Read each value from the expression, if it's a number, put it on the stack. Although its likely, that some file extensions may be missing from the list of associated file extensions with the application, yet they can be opened, be part of, or otherwise be associated with the program. Erlang default file extension associations.appup.beam

[PDF] RefactorErl 0.9.12.01 Manual, an _h postfix to the name. Usually, Erlang source files (having the extension .​erl) are loaded The following example outputs all defined functions with line numbers to a file ls_backups() - returns a lists of backups, that has been created before that provide the actual parameters to the call according to the given order. The module supports raw filenames in the way that if a binary is present, or the filename cannot be interpreted according to the return value of file:native_name_encoding/0, a raw filename is also returned.

Comments
  • And if there is another (new) file '4532.21.html'?
  • @TomRegner, sorry, I don't get what you mean.
  • I get it now - sorry for the too quick downvote; if you slightly edit the answer I'll be able to retract it -- I read the OP as allowing different prefixes, didn't realize they were all the same.
  • Thanks - I'm awfully sorry that happened.
  • @JonRiel, this is just a comparison for strings. They compare symbol and symbol lexicographically so '0' < '9' and it work as numerical comparison when length are equal. Btw, there is a special chat room for Erlang. You can ask there any question about Erlang.