parser.sortByValue(finalMap);
returns a new (sorted) map which you ignore. Instead, you print the old (unsorted) finalMap
.
finalMap = parser.sortByValue(finalMap);
should fix this.
Also note that the code above creates one map per file but prints only the last one of all of them.
Depending on what you want, you should either pass the map into parser.parseFile(str);
to accumulate the results for all files or sort+print inside the loop to get one result per file.