Sorting by Tags column causes some items to disappear and some to duplicate #6226

Closed
opened 2025-12-29 19:38:16 +01:00 by adam · 9 comments
Owner

Originally created by @markkuleinio on GitHub (Mar 18, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.1.9

Python version

3.8

Steps to Reproduce

  1. Create two tags: Tag1 and Tag2
  2. Create four prefixes: 10.99.0.0/24, 10.99.1.0/24, 10.99.2.0/24 and 10.99.3.0/24
  3. Assign tags Tag1 and Tag2 to prefixes 10.99.0.0/24 and 10.99.1.0/24
  4. Go to Prefixes, filter with "search within: 10.99.0.0/16" (this is not required if there is no other data in the database, this is just to be able to recognize the problem)
  5. Use Configure Table and add Tags column
  6. See all four prefixes listed, the first two prefixes with the tags
  7. Click Tags column to sort by tags

Expected Behavior

All four prefixes are shown and they are sorted by the tags.

Observed Behavior

Only 10.99.0.0/24 and 10.99.1.0/24 are shown, and they are both shown twice. 10.99.2.0/24 and 10.99.3.0/24 are not shown.

All four prefixes are shown again if the view is sorted by some other column than tags.

Sorting by tags seems to work when only one tag is assigned to the objects.

This same problem is also with at least devices so it is not related to prefixes view only.

Originally created by @markkuleinio on GitHub (Mar 18, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.1.9 ### Python version 3.8 ### Steps to Reproduce 1. Create two tags: Tag1 and Tag2 2. Create four prefixes: 10.99.0.0/24, 10.99.1.0/24, 10.99.2.0/24 and 10.99.3.0/24 3. Assign tags Tag1 and Tag2 to prefixes 10.99.0.0/24 and 10.99.1.0/24 4. Go to Prefixes, filter with "search within: 10.99.0.0/16" (this is not required if there is no other data in the database, this is just to be able to recognize the problem) 5. Use **Configure Table** and add **Tags** column 6. See all four prefixes listed, the first two prefixes with the tags 7. Click **Tags** column to sort by tags ### Expected Behavior All four prefixes are shown and they are sorted by the tags. ### Observed Behavior **Only 10.99.0.0/24 and 10.99.1.0/24 are shown, and they are both shown twice. 10.99.2.0/24 and 10.99.3.0/24 are not shown.** All four prefixes are shown again if the view is sorted by some other column than tags. Sorting by tags seems to work when only one tag is assigned to the objects. This same problem is also with at least devices so it is not related to prefixes view only.
adam added the type: bugstatus: accepted labels 2025-12-29 19:38:16 +01:00
adam closed this issue 2025-12-29 19:38:16 +01:00
Author
Owner

@jeremystretch commented on GitHub (Mar 18, 2022):

I don't think it should be possible to sort objects by tags. How would that even work? We could sort by the number of assigned tags I suppose, though that seems unlikely to be useful.

@jeremystretch commented on GitHub (Mar 18, 2022): I don't think it should be possible to sort objects by tags. How would that even work? We could sort by the _number_ of assigned tags I suppose, though that seems unlikely to be useful.
Author
Owner

@markkuleinio commented on GitHub (Mar 18, 2022):

It works by getting the list of objects grouped by their tags (just like when sorting by tenant for example). For example, if I have tags "Batch 1" and "Batch 2" (as well as untagged items), I get the output sorted by the batches, and I get immediate overview of the proceeding of the projects.

I would expect the sorting to work alphabetically by tag names (natural sorting for tags containing numbers would be great but maybe hard).

When there are multiple tags for an object, I would prefer to have the tags shown as sorted, now they are shown in random order (maybe by the order they were added to the object), but that is a different issue I guess.

@markkuleinio commented on GitHub (Mar 18, 2022): It works by getting the list of objects grouped by their tags (just like when sorting by tenant for example). For example, if I have tags "Batch 1" and "Batch 2" (as well as untagged items), I get the output sorted by the batches, and I get immediate overview of the proceeding of the projects. I would expect the sorting to work alphabetically by tag names (natural sorting for tags containing numbers would be great but maybe hard). When there are multiple tags for an object, I would prefer to have the tags shown as sorted, now they are shown in random order (maybe by the order they were added to the object), but that is a different issue I guess.
Author
Owner

@jeremystretch commented on GitHub (Mar 18, 2022):

I mean, what would be the practical use of sorting objects by the tags they have applied? There's no implicit relation between any two given tags, so it doesn't seem like it have any real value except in very niche scenarios.

@jeremystretch commented on GitHub (Mar 18, 2022): I mean, what would be the practical use of sorting objects by the tags they have applied? There's no implicit relation between any two given tags, so it doesn't seem like it have any real value except in very niche scenarios.
Author
Owner

@markkuleinio commented on GitHub (Mar 18, 2022):

I may have worded my use case badly (sorry, English is not my first language), I'll try again:

I have devices with tags "Batch 1" and "Batch 2" and so on (as well as untagged devices, and a few devices have other tags as well), so when I click the Tags column I get the device list sorted by the batches, and I get immediate overview of the proceeding of the project (by seeing the device statuses, and which devices still have specific tags assigned).

This has been extremely useful ever since the Tags column was made possible.

Edit: to be clear, this is the problem why I opened the issue: It is only now I realized that the output is mangled when sorted by tags (some prefixes/devices are totally missing, and some are listed twice). I suspect this problem started with NetBox 3.x because this system was upgraded from 2.11.x to 3.1.x only in January.

@markkuleinio commented on GitHub (Mar 18, 2022): I may have worded my use case badly (sorry, English is not my first language), I'll try again: I have devices with tags "Batch 1" and "Batch 2" and so on (as well as untagged devices, and a few devices have other tags as well), so when I click the Tags column I get the device list sorted by the batches, and I get immediate overview of the proceeding of the project (by seeing the device statuses, and which devices still have specific tags assigned). This has been extremely useful ever since the Tags column was made possible. **Edit: to be clear, this is the problem why I opened the issue: It is only now I realized that the output is mangled when sorted by tags (some prefixes/devices are totally missing, and some are listed twice).** I suspect this problem started with NetBox 3.x because this system was upgraded from 2.11.x to 3.1.x only in January.
Author
Owner

@DanSheps commented on GitHub (Mar 18, 2022):

I think the problem you will run up against here is, lets say you have your batch tags, and you want to sort by the tags. How do you determine which tag to sort by?

A practical example:

Tags:

Batch 1
Batch 2
Alpha
Beta

Device:

Device 1 (Batch 2,Alpha)
Device 2 (Batch 1, Beta)
...
Device X

If we go strictly by the alphanum of any tag, then it is going to sort Alpha before your Batch 1.

I just tried sorting with 'tags__name' using the Django Python API, and the sort doesn't actually work properly (likely sorts on the lowest keyed name only or something) so I doubt this can be accomplished without the addition of another dependency (m2msorted), however for such a niche thing, I don't think adding a dependency would be recommended. I think sorting by count would be the best way honestly.

@DanSheps commented on GitHub (Mar 18, 2022): I think the problem you will run up against here is, lets say you have your batch tags, and you want to sort by the tags. How do you determine which tag to sort by? A practical example: Tags: Batch 1 Batch 2 Alpha Beta Device: Device 1 (Batch 2,Alpha) Device 2 (Batch 1, Beta) ... Device X If we go strictly by the alphanum of **any** tag, then it is going to sort Alpha before your Batch 1. I just tried sorting with 'tags__name' using the Django Python API, and the sort doesn't actually work properly (likely sorts on the lowest keyed name only or something) so I doubt this can be accomplished without the addition of another dependency (m2msorted), however for such a niche thing, I don't think adding a dependency would be recommended. I think sorting by count would be the best way honestly.
Author
Owner

@markkuleinio commented on GitHub (Mar 18, 2022):

I have assumed that each column would be sorted according to the visible contents of the column. For example, a device with tags "Alpha" and "Batch 2" would be sorted before device with tag "Batch 1".

@markkuleinio commented on GitHub (Mar 18, 2022): I have assumed that each column would be sorted according to the visible contents of the column. For example, a device with tags "Alpha" and "Batch 2" would be sorted before device with tag "Batch 1".
Author
Owner

@jeremystretch commented on GitHub (Mar 22, 2022):

I have assumed that each column would be sorted according to the visible contents of the column. For example, a device with tags "Alpha" and "Batch 2" would be sorted before device with tag "Batch 1".

Right, which largely defeats the purpose. Why would I sort e.g. devices with the tag added-to-nms before rma-opened, for example? Not only does it impose a significant performance penalty, it doesn't seem useful practically.

@jeremystretch commented on GitHub (Mar 22, 2022): > I have assumed that each column would be sorted according to the visible contents of the column. For example, a device with tags "Alpha" and "Batch 2" would be sorted before device with tag "Batch 1". Right, which largely defeats the purpose. Why would I sort e.g. devices with the tag `added-to-nms` before `rma-opened`, for example? Not only does it impose a significant performance penalty, it doesn't seem useful practically.
Author
Owner

@markkuleinio commented on GitHub (Mar 23, 2022):

Why would I sort e.g. devices with the tag added-to-nms before rma-opened

There are also current use cases where column sorting is used for getting the output nicely grouped by the column content, in this case by the assigned tags.

I understand from the previous comments that there are performance or magnitude of work implications that prevent implementing multiple-tag sorting ideally and completely. Would it be feasible to automatically disable (in those cases where incorrect results occur = when multiple tags are present) the column sorting possibility, to prevent users getting false data as sorting result? With single tags only it works and is valuable for our workflows, so I wouldn't favour totally disabling Tags column sorting due to this issue.

@markkuleinio commented on GitHub (Mar 23, 2022): > Why would I sort e.g. devices with the tag added-to-nms before rma-opened There are also current use cases where column sorting is used for getting the output nicely grouped by the column content, in this case by the assigned tags. I understand from the previous comments that there are performance or magnitude of work implications that prevent implementing multiple-tag sorting ideally and completely. Would it be feasible to automatically disable (in those cases where incorrect results occur = when multiple tags are present) the column sorting possibility, to prevent users getting false data as sorting result? With single tags only it works and is valuable for our workflows, so I wouldn't favour totally disabling Tags column sorting due to this issue.
Author
Owner

@jeremystretch commented on GitHub (Mar 24, 2022):

Well, I see two issues here:

  1. Currently, attempting to order by tags corrupts the object list. This must be fixed.
  2. There is a desire to allow ordering result by tags. This might be implemented.

The first item can be addressed by simply disabling ordering for tag columns, for now. That will at least resolve the harmful behavior ASAP.

Let's handle the second point separately in a feature request, as there's no immediate need to implement the functionality. You (or anyone else) are welcome to submit a separate FR to implement reliable ordering by assigned tags, but it will need to include a workable proposed implementation.

@jeremystretch commented on GitHub (Mar 24, 2022): Well, I see two issues here: 1. Currently, attempting to order by tags corrupts the object list. This **must** be fixed. 2. There is a desire to allow ordering result by tags. This **might** be implemented. The first item can be addressed by simply disabling ordering for tag columns, for now. That will at least resolve the harmful behavior ASAP. Let's handle the second point separately in a feature request, as there's no immediate need to implement the functionality. You (or anyone else) are welcome to submit a separate FR to implement reliable ordering by assigned tags, but it will need to include a workable proposed implementation.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6226