user2047507
user2047507

Reputation: 1

How to reverse an array?

XXXXXVO[] general = XXXX.getXXXX();

 Comparator comparator = new Comparator() {
        DateFormat f = new SimpleDateFormat("dd/MM/yyyy");
        public int compare(Object obj1, Object obj2) {

            XXXXX gen11 = (XXXXXVO) obj1;
            XXXXX gen22 = (XXXXXVO) obj2;

            String firstString = gen11.getXXXXX().toString();
            String secondString = gen22.getXXXX().toString();

            if (secondString == null || firstString == null) {
                return 0;
            }


            return f.parse(firstString).compareTo(f.parse(secondString));

        }
    };      

    Arrays.sort(general, comparator);

Above is the code which sort the array general in ascending order. I want that in descending order. How can I reverse this in JDK 1.4??

Upvotes: 0

Views: 723

Answers (2)

Serhii Shevchyk
Serhii Shevchyk

Reputation: 39456

Just reverse the order in which you compare:

return f.parse(secondString).compareTo(f.parse(firstString))

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49432

I have not gone through the entire code. What I understood is you need to reverse the order , so multiply the result of compareTo() with -1.

return -1*f.parse(firstString).compareTo(f.parse(secondString));

To sort based on dates , you have to convert the String to Date in the Comparator which you are already doing using f.parse(firstString) and use Date's compareTo().

Upvotes: 1

Related Questions