1

So i have this ArrayList:

list.get(0) == "love"  
list.get(1) == "foo"  
list.get(2) == "make"  
list.get(3) == "links"

What i want is:

list.get(0) == "links"  
list.get(1) == "make"  
list.get(2) == "foo"  
list.get(3) == "love"

I have tried this but not working:

public static void orderDescending(final ArrayList<String> list){
    Collections.sort(list, new Comparator<String>() {
        public int compare(String s1, String s2) {
            Integer i1 = list.indexOf(s1);
            Integer i2 = list.indexOf(s2);
            return i1.compareTo(i2);
        }
    });
 }
6
  • 2
    what you're showing here is not sorting, but reversing... did you want to reverse the list? Commented Apr 11, 2012 at 6:35
  • yes i want to reverse it. Sorry. Commented Apr 11, 2012 at 6:37
  • If your intention is to reverse the elements then use this Collections.reverse(list). Commented Apr 11, 2012 at 6:38
  • yeah, i agree with @Hristo. What you want is reversing the arraylist. Commented Apr 11, 2012 at 6:38
  • 1
    Although I have to say, I respect the audacity of sorting with a comparator based on the index of the item in the list. Commented Apr 11, 2012 at 6:39

4 Answers 4

7

You can do this:

Collections.reverse(list);
Sign up to request clarification or add additional context in comments.

Comments

1

Simply,

Collections.reverse(list);

Comments

0

Here's a simple list reversal algorithm. If you don't care about the algorithm, use Collections.reverse().

public static void reverse(List<String> list) {

    if (list == null) {
        return;
    }

    int size = list.size();
    int mid = list.size() / 2;

    ListIterator<String> fwd = list.listIterator();
    ListIterator<String> rev = list.listIterator(size);

    for (int i = 0; i < mid; i++) {
        String tmp = fwd.next();
        fwd.set(rev.previous());
        rev.set(tmp);
    }
}

Comments

0

You did a small mistake otherwise you have almost done it.

You should have tried "return i2.compareTo(i1);"

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Reverse {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("love");
        list.add("foo");
        list.add("make");
        list.add("links");
        System.out.println(list);
        orderDescending(list);
        System.out.println(list);
    }

    public static void orderDescending(final ArrayList<String> list) {
        Collections.sort(list, new Comparator<String>() {
            public int compare(String s1, String s2) {
                Integer i1 = list.indexOf(s1);
                Integer i2 = list.indexOf(s2);
                // return i1.compareTo(i2);
                return i2.compareTo(i1);
            }
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.