Search code examples
umlplantuml

Automatically sort class items in PlantUML


Sometimes it is very tiresome to sort keys alphabetically, especially in larger tables, which grow over time.

Let's look at this diagram: enter image description here

@startuml
!define Table(name,desc) class name as "desc" << (T,white) >>
!define primary_key(x) <b><color:b8861b><&key></color> x</b>
!define column(x) <color:black><&media-record></color> x

Table(testTable, "demoTable") {
column(zzz)
column(aaaa)
column(gggg)
}
@enduml

Is there any option for PlantUML that would sort my column entries based on their names? I want to display the diagram as if I'd have written it as enter image description here

@startuml
!define Table(name,desc) class name as "desc" << (T,white) >>
!define primary_key(x) <b><color:b8861b><&key></color> x</b>
!define column(x) <color:black><&media-record></color> x

Table(testTable, "demoTable") {
column(aaaa)
column(gggg)
column(zzz)
}
@enduml

Is it possible with built-in tools, or do you have any idea how to accomplish this?


Solution

  • You could suggest the feature to the PlantUML Q&A Forum.

    In the mean time, why not use an !include and sort the items in your operating system. Here's a sample:

    @startuml
    !define Table(name,desc) class name as "desc" << (T,white) >>
    !define primary_key(x) <b><color:b8861b><&key></color> x</b>
    !define column(x) <color:black><&media-record></color> x
    
    Table(testTable, "demoTable") {
    !include items.include
    }
    
    Table(testTableSorted, "demoTable(sorted)") {
    !include sorted_items.include
    }
    @enduml
    

    enter image description here

    You could always keep the file sorted, or sort a copy of it:

    sort items.include > sorted_items.include