Solved Enum Sorting by variable name.

Discussion in 'Plugin Development' started by alex123099, Sep 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    alex123099

    Hi all!
    I have an enum called VPCommand, in which I have all my commands of the plugin. Each command has some strings which describe it and such. What I'm looking to do is to get an array which will contain all the commands of type VPCommand, but sorted alphabetically.
    This is my class:
    Code:
    package constants;
     
    import java.util.Arrays;
    import java.util.SortedSet;
    import java.util.TreeSet;
     
    import org.bukkit.ChatColor;
     
    public enum VPCommand{
    buy("p", ChatColor.GREEN + "[buy] [item name/id]" + ChatColor.BLUE + " - buys [item name/id] for its price."),
     
    give("p", ChatColor.GREEN + "[give] [player] [points]" + ChatColor.BLUE + " - gives [player] [points] points."),
     
    bind("p", ChatColor.GREEN + "[bind] [command]" + ChatColor.BLUE + " - binds [command] to an item. Left/Right click item to execute command.",
    ChatColor.GREEN + "[bind] " + ChatColor.BLUE + " - cancel command bind on held item."),
     
    mp("p", ChatColor.GREEN + "[mp]" + ChatColor.BLUE + " - shows how much mana you have left."),
     
    heal("p", ChatColor.GREEN + "[heal]" + ChatColor.BLUE + " - Heals players around you."),
     
    strike("p", ChatColor.GREEN + "[strike]" + ChatColor.BLUE + " - Strikes players around you."),
     
    armageddon("p", ChatColor.GREEN + "[armageddon]" + ChatColor.BLUE + " - Fire bursts around you."),
     
    shout("p", ChatColor.GREEN + "[shout]" + ChatColor.BLUE + " - FUS ROH DA!"),
     
    storm("p", ChatColor.GREEN + "[storm]" + ChatColor.BLUE + " - Control the wrath of the Gods."),
     
    blink("p", ChatColor.GREEN + "[blink]" + ChatColor.BLUE + " - Teleport to a random location in a blink of an eye."),
     
    tp("p", ChatColor.GREEN + "[tp]" + ChatColor.BLUE + " - Teleport forward a certain distance."),
     
    cloak("p", ChatColor.GREEN + "[cloak]" + ChatColor.BLUE + " - Disappear from space and time. Use again to disable."),
     
    explosion("p", ChatColor.GREEN + "[explosion]" + ChatColor.BLUE + " - Explode like a creeper, but be able to survive."),
     
    repel("p", ChatColor.GREEN + "[repel]" + ChatColor.BLUE + " - Push everyone in a certain radius back from you."),
     
    show("b", ChatColor.GREEN + "[show] [player]" + ChatColor.BLUE + " - shows how much VP [player] has.",
    ChatColor.GREEN + "[show]" + ChatColor.BLUE + " - shows how much VP you have."),
     
    add("b", ChatColor.GREEN + "[add] [name] [points]" + ChatColor.BLUE + " - adds [points] points to [name]."),
     
    set("b", ChatColor.GREEN + "[set] [name] [points]" + ChatColor.BLUE + " - sets [points] points to [name]."),
     
    shop("b", ChatColor.GREEN + "[shop] [page]" + ChatColor.BLUE + " - shows all the items in the shop.",
    ChatColor.GREEN + "[shop] [search] [filter]" + ChatColor.BLUE + " - search the shop."),
     
    addshopitem("b", ChatColor.GREEN + "[addshopitem] [item] [price] [desc]" + ChatColor.BLUE + " - adds [item] to the shop for [price]."),
     
    removeshopitem("b", ChatColor.GREEN + "[removeshopitem] [item name]" + ChatColor.BLUE + " - removes [item name] from the shop."),
     
    clearshop("b", ChatColor.GREEN + "[clearshop]" + ChatColor.BLUE + " - clears the shop."),
    history("b", ChatColor.GREEN + "[history]" + ChatColor.BLUE + " - shows shopping history.",
    ChatColor.GREEN + "[history] [player]" + ChatColor.BLUE + " - shows shopping history of [player]"),
     
    historyplayers("b", ChatColor.GREEN + "[historyplayers] [page]" + ChatColor.BLUE + " - gets a list of players with shopping history."),
     
    removeplayerhistory("b", ChatColor.GREEN + "[removeplayerhistory] [player]" + ChatColor.BLUE + " - removes [player]'s shopping history data."),
     
    changename("b", ChatColor.GREEN + "[changename] [old] [new]" + ChatColor.BLUE + " - changes [old] item name to [new] item name.",
    ChatColor.GREEN + "[changename] [old id] [new]" + ChatColor.BLUE + " - changes item with [old id] to [new] item name."),
     
    changeprice("b", ChatColor.GREEN + "[changeprice] [item name] [new price]" + ChatColor.BLUE + " - changes price of [item name] to [new price].",
    ChatColor.GREEN + "[changeprice] [item id] [new price]" + ChatColor.BLUE + " - changes price of [item id] to [new price]."),
     
    changedesc("b", ChatColor.GREEN + "[changedesc] [item name] [new desc]" + ChatColor.BLUE + " - changes [item name]'s description to [desc].",
    ChatColor.GREEN + "[changedesc] [item id] [new desc]" + ChatColor.BLUE + " - changes [item id]'s description to [desc]."),
     
    togglemp("b", ChatColor.GREEN + "[togglemp]" + ChatColor.BLUE + " - toggles mana regeneration."),
     
    update("b", ChatColor.GREEN + "[update]" + ChatColor.BLUE + " - downloads VotePoints update. Update applied on restart."),
     
    reloadconfig("b", ChatColor.GREEN + "[reloadconfig]" + ChatColor.BLUE + " - reload all VotePoints' config files."),
     
    checkupdate("b", ChatColor.GREEN + "[checkupdate]" + ChatColor.BLUE + " - Check if an update is required"),
     
    listpurchasable("b", ChatColor.GREEN + "[listpurchasable]" + ChatColor.BLUE + " - Lists all the items that can be sold in the store."),
     
    help("b", ChatColor.GREEN + "[help or ?] [page]" + ChatColor.BLUE + " - shows help page.");
     
    private String[] helpMsgs;
    private String allowedSender; // p = player, c = console, b = both
     
    private VPCommand(String allowedSender,String... helpMsgs){
    this.allowedSender = allowedSender;
    this.helpMsgs = helpMsgs;
    }
     
    public int getHelpMsgsLength(){
    return helpMsgs.length;
    }
     
    public String getHelpMsg(int msgIndex){
    if(msgIndex<0)
    msgIndex=0;
    else if(msgIndex>helpMsgs.length)
    msgIndex=helpMsgs.length-1;
     
    return helpMsgs[msgIndex];
    }
     
    public String getAllowedSender(){
    return allowedSender;
    }
     
    public static VPCommand[] getSortedCmdArr(){
    VPCommand[] cmds = values();
            Arrays.sort(cmds);
            return cmds;
    }
    }
    
    And this is the function I use for sorting:
    Code:
    public static VPCommand[] getSortedCmdArr(){
    VPCommand[] cmds = values();
            Arrays.sort(cmds);
            return cmds;
    }
    
    I have also tried using sortedMap and sortedSet, but the results were the same -> it is not sorted alphabetically. It is sorted somehow, I just don't know how and it is not what I need.
    So how would you go about sorting an enum alphabetically?
     
  2. Instead of putting the enum constants into an array, try getting using their String equivalent instead. You can easily access the constants via the valueOf method afterwards.
     
  3. Offline

    alex123099

    Code:
    public static VPCommand[] getSortedCmdArr(){
    String[] cmds = new String[values().length];
    for(int i=0; i<cmds.length; i++)
    cmds[i] = values()[i].toString();
            Arrays.sort(cmds);
            VPCommand[] vpcmds = new VPCommand[cmds.length];
            for(int i=0; i<vpcmds.length; i++)
           vpcmds[i] = valueOf(cmds[i]);
            return vpcmds;
    }
    
    I am unable to test right now, and am unsure if this is what you meant?
     
Thread Status:
Not open for further replies.

Share This Page