0

So I seemed to have run into a slight problem. I am attempting to store some doubles with a string (acting like a name for the them), then be able to sort them in descending order.

I would also like to store them in someway, like a hashmap, arraylist, list etc... I'm not to sure on which would be the best.

Kind of like Lets assume these are stored in some way like a hashmap, list etc...

Bob: 6.0
Mary: 5.4
Bill: 6.3
Ann: 5.0
Jim: 6.0

Then output them to something like:

Bill: 6.3
Bob: 6.0 //Notice, it must be able to support duplicates. 
Jim: 6.0
Mary: 5.4
Ann: 5.0

I hope this makes sense to others, if not let me know and I can try to clear it up.

P.S. I looked for other threads like this but could not find one that suits my needs.

Edit: I seemed to of found a way that works somehow... If you are interested in seeing, I have a pastebin link here: http://pastebin.com/qWJbD5MZ

Now, the code is built off the Bukkit API, so It may not be useful to others as much as it could.

Jack Sullivan
  • 91
  • 1
  • 1
  • 9

3 Answers3

9

The easiest way is to create a wrapper class:

class Person {
    String name;
    double score;
    //constructor, getters etc.
}

Then put the persons in a list:

List<Person> list = Arrays.asList(new Person("Bob", 6),
                                  new Person("Mary", 5.4),
                                  new Person("Bill", 6.3),
                                  new Person("Ann", 5.0),
                                  new Person("Jim", 6.0));

And finally sort them with a custom comparator that compare the scores:

Collections.sort(list, comparator);

The comparator could look like:

Collections.sort(list, new Comparator<Person>() {

    @Override
    public int compare(Person o1, Person o2) {
        //sort by score
        if (o1.score != o2.score)
            return Double.compare(o1.score, o2.score);
        //if same score, sort by name
        return o1.name.compareTo(o2.name);
    }
});
assylias
  • 321,522
  • 82
  • 660
  • 783
  • How about some pointer on what the comparator would look like? – Gabe Aug 08 '13 at 06:25
  • @Gabe I thought I'd leave it as an exercise. But here it is. – assylias Aug 08 '13 at 06:36
  • @assylias You should leave it as an exercise. The hints is more than enough for a normal developer to further find out the answer. – Adrian Shum Aug 08 '13 at 06:41
  • @AdrianShum Yes well - it's there now so... – assylias Aug 08 '13 at 06:43
  • @AdrianShum: If this were homework, I'd say to leave it as an exercise. In this case, putting the code here probably saves an hour or two of a novice trying to figure it out. I'd have been satisfied with a simple link saying how to write it, though. – Gabe Aug 08 '13 at 07:03
2

If you don't need to be able to lookup a value by key, the easiest way to do this is to probably define a type which has a name and integer property, and then have it implement the java.lang.Comparable interface by comparing the integer values, and storing the data in a TreeSet to get a sorted collection.

If you want to have the values be able to be looked up by the string value and also have them sorted by integer you will probably have to combine two data structures. You can do this by storing the data in a HashMap while also having a sorted collection, though you would have to do some work to keep the data in sync between the two structures.

0

You can make a use of Arrays.sort method

public class Test {
  public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
     Person[] personArray = {new Person("Bob", 6), new Person("Mary", 5.4), new Person("Bill", 6.3),new Person("Ann", 5.0),new Person("Jim", 6.0)};

    Arrays.sort(personArray,new MyComparator());
    for(Person person:personArray){
        System.out.println(person.getName()+"  : "+person.getScore());
    }

}

}
class Person {
 String name;
 double score;

Person(String name, double score) {
    this.name=name;
    this.score = score;

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getScore() {
    return score;
}

public void setScore(double score) {
    this.score = score;
}

}
class MyComparator implements Comparator<Person> {

@Override
public int compare(Person o1, Person o2) {
    return o1.name.compareTo(o2.name);
}

}
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24