question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ordering Problem with recursive method

See original GitHub issue

Hi!

I’ve a problem when execute TreeNode Model with nested list, the Tree isn´t respect the order iteration, for example:

FAKE_PAYLOAD = [
    { 'name': "LEGAL",
      'children': [{ 'name': "CORPORATIVO",
                     'children': [{ 'name': "Sub-SubFolder 1" },
                                  { 'name': "Sub-SubFolder 2" },
                                  { 'name': "Sub-SubFolder 3" },
                                  { 'name': "Sub-SubFolder 4" },
                                  { 'name': "Sub-SubFolder 5" },
                                  { 'name': "Sub-SubFolder 6" },
                                  { 'name': "Sub-SubFolder 7" },
                                  { 'name': "Sub-SubFolder 8" },
                                  { 'name': "Sub-SubFolder 9" },
                                  { 'name': "Sub-SubFolder 10" },
                                  { 'name': "Sub-SubFolder 11" },
                                  { 'name': "Sub-SubFolder 12" },
                                  { 'name': "Sub-SubFolder 13" },
                                  { 'name': "Sub-SubFolder 14" },
                                  { 'name': "Sub-SubFolder 15" },
                                  ] },
                   { 'name': "CONTRACTUAL",
                     'children': [
                         { 'name': "Sub-SubFolder 1" },
                         { 'name': "Sub-SubFolder 2" }] },
                   { 'name': "GOBIERNO",
                     'children': [{ 'name': "Sub-SubFolder 1" }] }
                   ] },
]

My model is like this:

class FakeFolder(TreeNodeModel):
    name = models.CharField(max_length=255)

    def __str__(self) -> str:
        return self.name

My recursive method is:

def write_tree(tree_list, parent = None):
    for folder in tree_list:
        folder_obj = FakeFolder.objects.create(name=folder['name'])
        if parent:
            folder_obj.set_parent(parent)
        if 'children' in folder:
            write_tree(folder['children'], folder_obj)

After, executed get_children() of root node, the list it seems wrong order:

FakeFolder.objects.all()[0].children
[<FakeFolder: CONTRACTUAL>, <FakeFolder: CORPORATIVO>, <FakeFolder: GOBIERNO>]

And the order of children_pks is ‘106,90,109’, when the correct order must be maybe ‘90,106,109’ or:

[<FakeFolder: CORPORATIVO>, <FakeFolder: CONTRACTUAL>, <FakeFolder: GOBIERNO>]

With this package it’s possible to set order manually? I don’t see a method for this. The only way that I’ve found is with the priority property but only works in descendent order

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aarondiazrcommented, May 11, 2020

Thanks, amm maybe if there are possible to order tree in based of custom property like a integer in ascending or descending order instead of alphabetically will be great. Because in the practice will need modify the order based on specific use case. Now I will override the get_children method to make possible different order…

I see behind of magic performance one weakness, for example if execute this on microservice or serveless, will always go to database because the memory location of cache is independently of any instance of docker, maybe a feature with redis will be great.

0reactions
fabiocaccamocommented, Aug 17, 2020

@aarondiazr just customize the tn_priority field value, then execute cls.update_tree().

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recursive Programming - Towards Data Science
Finally, recall that knowing your ordering is the most important step to solving a recursive problem, and your aim is always to cover...
Read more >
Recursive Bubble Sort - GeeksforGeeks
Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong ......
Read more >
Recursive Sorting Algorithms – Digilent Blog
Mergesort is a divide-and-conquer algorithm that divides an array of length n into n subarrays, and then recombines them using merge. Our ...
Read more >
Examples of Recursion: Recursion in Sorting | SparkNotes
Recursive techniques can be utilized in sorting algorithms, allowing for the sorting of n elements in O(nlogn) time (compared with the O(n 2...
Read more >
Recursion and Sorting - Learneroo
The key idea behind Quick Sort is to compare a list of elements to one element, partition the list to less-than and greater-than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found