Update Solved the compiling error, now the only problem with the code is how to sort the dict alphabetically for pretty printing.
I am refactoring an argument parser from python into Genie, however I found myself stuck in how to sort the items form a dict before appending them to a list.
In python it is as simple as:
lines.append("Options:")
if len(self.options):
for name, option in sorted(self.options.items()):
lines.append(" %s: %s" % (name, option.values))
else:
lines.append(" [none]")
self.options is declared as self.options = {}
Now how can print the contents of the dict, but sorted?
Here is the code where I am stuck:
def ListOptions()
var lines = new list of string
lines.add("Options:")
if _options.size != 0
for name in _options.keys
lines.add(" %s: %s" % (name, _options.values))
else
lines.add(" [none]")
ListOptions is a method within a class, and I declared _options as _options:new dict of string, string
There is no compiling error in that section of the code anymore. My question is how to sort the elements of the dict before adding them to the list lines
?
Based on Thomas and Jens comments, one can also use TreeMap. Here is how it would look:
[indent=4]
uses
Gee
init
var dic = new TreeMap of string, string
dic["z"] = "23"
dic["abc"] = "42"
dic["pi"] = "3.141"
for k in dic.ascending_keys
print (@"$k: $(dic[k])")