I’d like to retrieve them by custom order, i.e. 1, 2.1., 2.2., 3, however, order_by retrieves it as 1, 3, 2.1, 2.2 …
Edit:
I guess what happens here is that i have numbers 1 and 3, and compounds 2.1. and 2.2 and ordering of ‘.’ is after numbers, but, somehow, i want it to use the first arg and second arg in the ‘.’ pair for ordering.
Is there a simple way to achieve a “natural” custom ordering by “indent”
If you are using library(solution_sequences), keep in mind that that is using findall/3 and sort/4, then member/2. You can do this explicitly (instead of using order_by/2) and replace the call to sort/4 with your specialized sorting.
Next thing: you have in the first argument of item/2 in your example “numbers”, but those are actually integers and floats. Normally, those are compared according to the standard order of terms, and really, they should sort as expected. Here:
But the second argument of item/2 from your example is not valid Prolog, unless you have somehow redefined the operators. In other words, your example is not reproducible; you need to provide a minimal reproducible example that includes whatever operators you might have defined.
The solution to your problem might involve a custom comparison predicate and predsort/3, but lets wait until you show the rest of your code.
What i actually did was to create a dot via atomic_list_concat/2, as i traverse a nested structure, something like this:
X=1, Y=1, atomic_list_concat([X, '.', Y], Term),
Not noticing that i am actually creating something that looks like a float, but actually is an atom.
The dot is merely a way to indicate “indenting” nesting, and can be exchanged for any other symbol:
I now wonder to use float as a means for generating nested, with the decimal dot indeed moved for each level … perhaps that’s the easiest way to get things in standard order.
Wonder if there is a way to translate an atom to a float
Edit: looks like atom_number/2 also works with float
Without going into whatever else you are doing, yes, there are many ways to do that. One obvious choice is atom_number/2. Here it is (stuffed inside a maplist so that I don’t have to type 4 different queries):
The sort/4 predicate. In particular, it takes a list in the first argument; you still need to know how the term you are sorting is nested. Not sure if that works for your use case.
The predsort/3 predicate. There you can do whatever you feel like. Just remember that if two terms compare equal, you will lose one of them. There is a trick where you make sure that equivalent elements do not compare equal with your comparison predicate, but it is a bit of a hack.
This is probably the cleanest solution if the first option doesn’t work for you: write a predicate that creates a key for your elements that sorts correctly using the standard order of terms.
For the last option, there is a short example in the docs for keysort/2.
I will look into this. I guess currently the nested structure is “context free” i.e. I have no access to the higher level nestings – only the last two – i guess i could pass along the path and use this to create a key for sorting.
The second and third option above are equivalent really. Those are two different approaches to the same problem. This SO Q/A goes a bit into it (it is a bit of a rabbit hole though, in terms of why the question was asked and why it was answered… if you want to understand, you’d have to read the question, all the answers, and probably follow the links… not sure if it is worth your time).
Similarly, first and third option are also equivalent. Read the docs to sort/4 and keysort/2, it is all written in there, in a fashion.
Sorry for the multiple replies. So actually, there is a very simple solution to this. Do not make an atom; just keep the list of integers as they are. That should sort correctly with the standard order of terms. So instead of 2.1.1 use [2, 1, 1], instead of 3.1 use [3, 1] and so on. This will sort correctly with sort/2, sort/4, msort/2, keysort/2…
I will leave the rest of my replies above for posterity. I think we have a great example of the XY problem and for some reason I found those delightful.
To avoid dealing with custom sorting, I redid the code a bit and now retrieve by sorted index, using between/3.
This now retrieves in a sorted manner each nested list item, but there is, naturally, quite a bit of backtracking for each list item retrieved (each list item has an index 1…list-length stored with it) – i think your solution with lists will be much more efficient.