arraylist sorting problem

Hello everybody,

I'm fairly new to Java, and my first real project which also implements jsp was going very well, until...

I had to sort an ArrayList, which holds object with only 2 attributes, a String uid, and an int diff.

I want to sort this ArrayList in ascending order by int diff.

So ArrayList can hold the following object attributes in that order:

Object 1:

uid: 12

diff: 18

Object 2:

uid: 2

diff: 24

Object 3:

uid: 32

diff: 22

After sorting the ArrayList i want to have it look like this:

Object 1:

uid: 12

diff: 18

Object 3:

uid: 32

diff: 22

Object 2:

uid: 2

diff: 24

I have read numerous posts everywhere on the net but nothing seems appropriate, just all too complicated. There has to be a simple solution because it doesn't seem so difficult...

Any help would be greatly appreciated!

Regards,

Bonomel

[984 byte] By [bonomela] at [2007-10-2 9:54:06]
# 1

> I have read numerous posts everywhere on the net but

> nothing seems appropriate, just all too complicated.

> There has to be a simple solution because it doesn't

> seem so difficult...

>

> Any help would be greatly appreciated!

>

> Regards,

> Bonomel

A simple way to solve this, is to implement the Comparable interface in your custom object and overriding the compareTo(Object) method.

Use Google to get the details after you studied this little example:

public class MyObject implements Comparable {

private String uid;

private int diff;

public MyObject(String uid, int diff) {

this.uid = uid;

this.diff = diff;

}

public int compareTo(Object obj) {

MyObject that = (MyObject)obj;

return this.diff - that.getDiff();

}

public int getDiff() {

return diff;

}

public String toString() {

return "["+uid+" - "+diff+"]";

}

}

And to test it:import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

public class ComparableDemo {

public static void main(String[] args) {

List list = new ArrayList();

list.add(new MyObject("12", 18));

list.add(new MyObject("2", 24));

list.add(new MyObject("32", 22));

System.out.println("The list before sorting: "+list);

Collections.sort(list);

System.out.println("The list after sorting: "+list);

}

}

Good luck.

prometheuzza at 2007-7-16 23:58:56 > top of Java-index,Other Topics,Algorithms...
# 2
Thank you very much for your detailed explanation! I will look into it as soon as I get up in the morning! You're really very helpful!
bonomela at 2007-7-16 23:58:56 > top of Java-index,Other Topics,Algorithms...
# 3
> Thank you very much for your detailed explanation! I> will look into it as soon as I get up in the morning!> You're really very helpful!You're welcome.
prometheuzza at 2007-7-16 23:58:56 > top of Java-index,Other Topics,Algorithms...
# 4
Thanks again, it works perfectly now!
bonomela at 2007-7-16 23:58:56 > top of Java-index,Other Topics,Algorithms...
# 5
> Thanks again, it works perfectly now!Nice of you to post it, and again: you're welcome.; )
prometheuzza at 2007-7-16 23:58:56 > top of Java-index,Other Topics,Algorithms...