Solved Get ConfigurationSection in order

Discussion in 'Spigot Plugin Development' started by djuitietr, Jul 22, 2018.

  1. I am using
    Code (Java):
    for (String string : getConfig().getConfigurationSection("Items").getKeys(false))
    to get contents of my list.

    Code (YAML):
    : 100
    : 150
    : 50
    : 250
    But when i do this the list doesn't listed in order. List must STONE, DIRT, COBBLESTONE, GRASS but it returns like COBBLESTONE, STONE, DIRT, GRASS.
  2. That's a bit odd? Could you please take some screenshots after running the following code:
    Code (Java):

    Code (Java):

    List<String> list = new ArrayList<>(getConfig().getConfigurationSection("Items").getKeys(false));
    I'm aware that the getConfig().getConfigurationSection("Items").getKeys(false) returns a Set<String> which doesn't order things (depending on implemention).

    I'm curious to see the outcome really.

    What makes it a bit more odd is the fact that it seems that the outcome it's giving you, is ordered by value (the numbers)

    EDIT: Full code would be nice by the way.
    #2 darklazerog, Jul 22, 2018
    Last edited: Jul 22, 2018
  3. Sort the List yourself. It being sorted by value is completely coincidental.
  4. I would expect the entries to keep its order as defined inside the file:
    * SnakeYaml mentions that it uses LinkedHashMap as default for Maps, and inside its code it even mentions that it intends to keep the order from the parsed document (
    * Bukkit uses LinkedHashMaps for the ConfigurationSection values and it should keep the order it gets from SnakeYaml intact ( When replacing values (without remove + add) it should also keep the order.
    * Bukkit uses a LinkedHashSet for the returned keys (, and it iterates section's values in order and doesn't re-insert/re-order entries.

    I also tested this, and no matter what order I tried in the config file, the keys were ordered accordingly.

    So maybe the issue lies somewhere else inside your code. Are you adding those keys or materials/values somewhere into a Map or Set or other type of unordered collection?
  5. Those assumptions are invalid in a formal context though, since nowhere in the documentation it's specified to be in order.

    If you want ordering, use a List of sections.
  6. Thank you guys i found the problem i was putting the keys to hashmap and then getting the keys from looping the hashmap and hashmap gives me the list non-regularly. So my new problem is how can i set keys to hashmap in order :D

    Edit: Sorry i didn't read the blablubbabc's comment i'll try LinkedHashMap.
  7. A HashMap has no particular internal order. Do not rely on configuration entries to be ordered, it you require it, do what @DarkSeraphim says and use a list of sections. Or rework your plugin to not rely on it.
    • Creative Creative x 1
  8. I don't understand you by saying list of sections but LinkedHashMap works fine thanks everyone problem solved.
  9. It's a hacky solution which might break whenever Bukkit or SnakeYaml changes their implementation.

    List of sections being the 'getMapList(String key)' method provided by ConfigurationSection:
    Code (YAML):
      - first
    : value
    : value
      - first
    : value
    : value
    If you don't want this, then there's no general way of getting keys ordered (while not relying on the implementation of two other projects). You could sort it if you want, but that doesn't support ordered lists in any random ordering.
    #9 DarkSeraphim, Jul 22, 2018
    Last edited: Jul 22, 2018
    • Informative Informative x 1
    • Useful Useful x 1