Solved Sort lore by colour codes.

Discussion in 'Spigot Plugin Development' started by Twixellzz, Aug 3, 2019.

  1. Hey guys,

    I'm trying to sort an items lore by checking colour codes. I'd like it in an order of &5, &e, &a, &7.

    I've tried looping through the lore and adding to a new list, but obviously it'll go from the first array and add it so you just get the same list, so what I'm really looking for is a method on how to do this.

    It might be simple and I'm just overthinking things, but my brain isn't working on this one, any help would be greatly appreciated :)

    Thank you.
  2. So are you trying to take an item that has a ton of lore and just pick out those specific color code lines?
  3. [​IMG]

    So I'd like to order all the lines by the predefined format above, so it's easier to read. Ignore the vanilla enchantments as they'll be added to the lore and removed using the HIDE_ENCHANTS itemflag
  4. I'd personally add all "enchants" (from lore) into separate collections. After having them all added into their own collection, I'd then re-add them the way I want. Using Collection#addAll perhaps.
    • Like Like x 1
  5. Sounds like the way to go, thanks mate!
  6. If you've resolved your problem, set the thread as solved.
  7. It'd be a better solution to sort the list through a custom Comparator, then using Collections::sort. Your comparator would just order the strings in the order you want.

    Code (Java):
    List<String> lore = ...;
    Collections.sort(lore, new MyCustomComparator());
    //sorted lore
    Then apply the lore again.

    In fact, you can avoid creating a separate comparator and just inline it via lambda by using the static Comparator method Comparator::comparing
    • Like Like x 1