Sorting an array for ie top scores

Discussion in 'Plugin Development' started by Randy Schouten, Oct 25, 2011.

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

    Randy Schouten

    So I have an array with for example: [a=0, b=4, c=2].
    What would be the best way to make it so I will have it sorted as a list and can print it like so: "b: 4".
    It will just go through the list with a for-loop then.

    Any ideas?
     
  2. Offline

    thehutch

    PHP:
    for (int x=<= list.size() ; x++) {
        
    player.sendMessage(": " list[x]);
    }
     
  3. Offline

    Randy Schouten

    That... is really not what I meant.
    The output would be "0: 0" then, and it's not sorted either.
    I want the output to be b: 4, c: 2, a: 0.

    Thanks for the time though. :)
     
  4. Offline

    coldandtired

    Create a class with the two properties for the letter and the score. Than put all your values into an ArrayList of that class. Then sort it with a comparator using the score property.
     
  5. Offline

    Randy Schouten

    I'm not sure what you mean.
    I'm sorry...

    Might it also be useful if I told you I get these properties from a hashmap?
    The key is player, the value is the score the player has.

    I'm trying to find out how to do this myself as well with google and such, but I have yet to find something I can use.
     
  6. Offline

    coldandtired

    This page should be exactly what you need.
     
  7. Offline

    mindless728

    The easiest solution is instead of using Arrays use a TreeSet and make a class that holds the player and the score in one node. Then when making the tree set you give it a custom comparator so that it knows how to sort your data (ie by score and nothing about the player, or if players are tied you can resolve that as well)
     
  8. Offline

    Father Of Time

    Here is an example of what everyone is talking about:

    Code:
        Person p = new Person("Bruce", "Willis");
            Person p1  = new Person("Tom", "Hanks");
            Person p2 = new Person("Nicolas","Cage");
            Person p3 = new Person("John","Travolta");
    
            ArrayList list = new ArrayList();
            list.add(p);
            list.add(p1);
            list.add(p2);
            list.add(p3);
    
            Collections.sort(list, new Comparator(){
    
                public int compare(Object o1, Object o2) {
                    Person p1 = (Person) o1;
                    Person p2 = (Person) o2;
                   return p1.getFirstName().compareToIgnoreCase(p2.getFirstName());
                }
    
            });
    
            System.out.println(list);
    You first make a Comparator, what this does is compares 2 items by criteria you set. You then call the List build in sort function and pass the list and comparator as a parameter. The Collection will automatically sort the order of your list by the conditions established in the comparator you wrote; then it would just be a matter of performing a normal for loop through your list.
     
Thread Status:
Not open for further replies.

Share This Page