Title: Veocity : How to find frequency of values in array and then sort the values in the order of frequency
Tags: arrays;sorting;velocity;apache-velocity
Question: I have any array in velocity which has duplicate values too. I want to create an array with unique values where all the values are sorted based on their count/frequency in the original array.
For example:
#set($y = 0)
#set ($pagelist = [])
#foreach($label in $page.labels)
#set($pinnedPages = $repository.getContentByLabel(&quot;$!label&quot;))
#set($currentpage = $page)

## Display the page that has a label that matches current page label.
#foreach($page in $pinnedPages)
#if($page != $currentpage)
## Display the page name as a list item.
#set ($pagelistswallow = $pagelist.add($page.title))
<li&gt;$pagelist.get($y)</li&gt;
#set($y = $y + 1)
#end
#end
#end
The pagelist array has page names and few of them occur multiple times in this array . I need an array with unique page names sorted in the order of their frequency in the original pagelist array.
Can anyone help? I have tried but my code is not working...


Here is another answer: You can do :
#foreach($label in $page.labels)
#set($pinnedPages = $repository.getContentByLabel(&quot;$!label&quot;))
#set($currentpage = $page)
## Prepare a map (title -&gt; occurrences) of pages
## which have a label that matches current page label.
#set($pagemap = {})
#foreach($subpage in $pinnedPages)
#if($subpage != $page)
#set($junk = $pagemap.put(
$page.title,
$pagemap.getOrDefault($page.title, 0) + 1))
#end
#end
## Calculate a map (occ, title) -&gt; title
#set($pagetmp = {})
#foreach($entry in $pagemap.entrySet())
#set($junk = $pagetmp.put(
&quot;${entry.value}_${entry.key}&quot;,
$entry.key))
#end
## Display those related pages by increasing
## number of occurrences (just reverse the array
## for a decreasing order)
#set($pagekeys = $pagetmp.keySet().stream().sorted().toArray())
#foreach($key in $pagekeys)
<li&gt;(label: $label) $pagetmp.get($key)</li&gt;
#end
The need to use sorted() explicitely comes from the fact that Velocity empty maps are LinkedHashMap, hence keys order is a hash, not the natural order.
As a variant, to be able to display the number of occurrences, you can easily adapt this code to have $pagetmp values be a map { occ, title }.
If you're using Velocity 1.6, you have to split the instruction #set($junk = ...) into :
#set($subinit = $pagemap.getOrDefault($pagetitle, 0) + 1)
#set($junk = $pagemap.put($page.title, $ubinit))
Comment for this answer: @Kapa I cannot easily test on Velocity 1.6. Can you tell me the error you get?
Comment for this answer: @Kaps I'm sorry, some copy paste operation failed during my last edit. This is the correct fix for Velocity 1.6, I didn't put the whole code but I think it's much clearer.
Comment for this answer: Thansk @claude! I am getting errors. Is it because I am using Velocity 1.6? The error among many is for this line -> $pagemap.getOrDefault($page.title, 0) + 1)).
Error: Was expecting one of: "," ... ")" ...
at org.apache.velocity.Template.process(Template.java:141)
Comment for this answer: Thanks @claude. I tried replacing the one line with the two lines you shared (ignoring the 3rd line which is the original line) but nothing worked. I tried all possibilities but in vain. Can you please update the original code with the update? I am for sure doing something wrong here in understanding what you meant. Thanks again so much!!
Comment for this answer: I will for sure. But can you please share the updated full code first. I think I am using the updated lines incorrectly. I want to be sure that I am using them correctly and then share the error with you. Thanks a ton!!
Comment for this answer: Thanks @Claude. It works now without error, but, the ouput is incorrect. It is now showing only the current page name against each label on the current page instead of showing other pages with the same label and in decreasing order of count of matched labels...
Here is how the page looks: https://pasteboard.co/JhpF8g2.png
Getting Started is the page name.
Comment for this answer: can you help me with the issue?
Comment for this answer: Can anyone help?
Comment for this answer: Is there any velocity champion who can help me?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:06/26/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<arrays><sorting><velocity><apache-velocity>', 'Title': 'Veocity : How to find frequency of values in array and then sort the values in the order of frequency', 'ViewCount': 225, 'id': 62593280, 'user_AboutMe': '', 'user_AccountId': 11983050.0, 'user_DisplayName': 'Kaps', 'user_Reputation': 23.0, 'user_Views': 5.0, 'user_id': 8768355.0}}