3

I have an ArrayList<List> where the Lists have string values that hold a name and then a double converted to a string.

Example:

List<String> list = New List; 
list.add("Abraham");
list.add(String.valueOf(0.65));

List<String> list2 = New List; 
list2.add("Bowers");
list2.add(String.valueOf(0.89));

ArrayList<List> arrayList = new ArrayList<>();
arrayList.add(list); 
arrayList.add(list2); 

How can I sort the ArrayList in descending order by the value of the double?

Duff
  • 410
  • 2
  • 13
  • 4
    Are you sure you don't want to use a `Map`? Why are you storing the `name` and the `value` in the same list? – brso05 Sep 30 '16 at 12:52
  • The List will contain more than two items and I would like to know the order of the names based off of related variables. – Duff Sep 30 '16 at 13:00
  • Ya what your describing (`key`, `value` pairs) is perfect for a `Map`...Look up `HashMap`. – brso05 Sep 30 '16 at 13:01
  • @BryceD check this out: http://stackoverflow.com/questions/780541/how-to-sort-a-hashmap-in-java – brso05 Sep 30 '16 at 13:02

4 Answers4

7

Use Collections.sort(arrayList, instanceOfYourOwnImplemenationOfComparator) after having a custom implementation of Comparator<ArrayList>.

Or better, Java is an Object oriented language so create a class dedicated to the storage of your String+double and make it comparable.

class MyClass implements Comparable<MyClass> {
   private String word;
   private double score;

   MyClass(String word, double score) {
       this.word = word;
       this.score = score;
   }

   @Override
   public int compareTo(MyClass o) {
      return (int) Math.round(Math.signum(score - o.score));
   }

}
YMomb
  • 2,366
  • 1
  • 27
  • 36
2

I would change your implementation and create a class containing two fields, a String and a Double one. This class would implement the Comparable interface and its compareTo would be based on the double alone. Something like

public class MyClass implements Comparable<MyClass> {
    private double value;
    private String name;

    /*Constructors, setters and getters*/

    public int compareTo(MyClass o) {
        return(new Double(value)).compareTo(myO.getValue));        
    }
}

Then, your code would become:

ArrayList<List> arrayList = new ArrayList<>();
arrayList.add(new MyClass("abraham",0.65)); 
arrayList.add(new MyClass("bowers", 0.89)); 

Collections.sort(arrayList);

I just typed the code, but I believe the idea is pretty straightforward.

I hope it helps.

rlinden
  • 2,053
  • 1
  • 12
  • 13
0

You should be using a class. Let's call it Foo

Then you would do

List<Foo> mylist = new ArrayList<Foo>();
mylist.add (new Foo("Abraham", 0.65);
mylist.add (new Foo("Bowers", 0.89);
// etc

I'll leave it as an excercise for you to write the Foo class. It should implement Comparable

class Foo implements Comparable {
// TODO

Finally

 Collections.sort(mylist);
Rodney
  • 2,642
  • 1
  • 14
  • 15
0

In Java 8 it may be done with streams like this

arrayList.stream()
    .sorted((l1, l2) -> Doulbe.valueOf(l1.get(1)) - Doulbe.valueOf(l2.get(1)))
    .collect(Collectors.toList())

Here l1 and l2 are your sublists and each 2nd elements of them are your numbers. The used comparator subtracts one number from another for making decision in what order your sublists should be put into output arrayList

Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41