Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

working with lists

Thread view: 
black-white - 01 Apr 2007 02:10 GMT
hello,

I use Vector or ArrayList to store my objects.
However, sometimes I need to sort those lists with considering just one
value. Is that possible?

For example:

Class a{
int value1;
int value2;
String name;
}
Vector myList=new Vector();
a object1=new a();
object1.fillValues(); //that gives some values to each variable...
myList.add(object1);
myList.add(object2);
.
.
.....
myList.add(object1000);

Now, how can I sort that list just considering value1 in all objects?
CodeForTea@gmail.com - 01 Apr 2007 03:03 GMT
> hello,
>
[quoted text clipped - 20 lines]
>
> Now, how can I sort that list just considering value1 in all objects?

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

public class SortOnOneValue implements Comparable  {
   int value1;
   int value2;
   public SortOnOneValue(int val1, int val2) {
       value1 = val1;
       value2 = val2;
   }

   public int compareTo(Object obj) {
       SortOnOneValue val = (SortOnOneValue) obj;
       return this.value1 - val.value1;
     }

   public String toString() {
       return "value1 = " + value1 + " value2 = " + value2;
   }

   /**
    * @param args
    */
   public static void main(String[] args) {
       ArrayList myList =new ArrayList();
       myList.add(new SortOnOneValue(1,5));
       myList.add(new SortOnOneValue(5,5));
       myList.add(new SortOnOneValue(2,5));
       System.out.println(myList);
       Collections.sort(myList);
       System.out.println(myList);

   }

}
Dinesh
black-white - 01 Apr 2007 10:31 GMT
Hello,

You misunderstood my question but thanks for answer anyway...

>> hello,
>>
[quoted text clipped - 57 lines]
> }
> Dinesh
Hendrik Maryns - 02 Apr 2007 10:52 GMT
black-white schreef:
> Hello,
>
> You misunderstood my question but thanks for answer anyway...

Please do not top-post.

I think /you/ misunderstood Dinesh’s solution.  Have another look at it,
it does exactly what you asked.  (You might want to have a look at
anonymous classes too, you can use one that implements Comparator in
Collections.sort(List, Comparator))

H.

>>> hello,
>>>
[quoted text clipped - 57 lines]
>> }
>> Dinesh

- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Patricia Shanahan - 01 Apr 2007 03:50 GMT
> hello,
>
[quoted text clipped - 20 lines]
>
> Now, how can I sort that list just considering value1 in all objects?

If the value1 order is the natural order for class a, make it implement
Comparable, and use the Collections sorts for lists of Comparable.

If there are several equally significant orders, create a Comparator for
each order, and use the Collections sorts with Comparator.

Patricia
Muntasir Azam Khan - 01 Apr 2007 09:34 GMT
> hello,
>
> I use Vector or ArrayList to store my objects.
> However, sometimes I need to sort those lists with considering just one
> value. Is that possible?
...
> Now, how can I sort that list just considering value1 in all objects?

Look up the Comparable and Comparator interfaces.
If you only need to sort on one value make your class implement
Comparable.
If you need to sort on different values at different times, create a
separate Comparator (i.e. a class that implements the Comparator
interface) for each value you are sorting on.
black-white - 01 Apr 2007 10:32 GMT
Muntasir and Patricia,

Thanks I will look up those interfaces...

>> hello,
>>
[quoted text clipped - 10 lines]
> separate Comparator (i.e. a class that implements the Comparator
> interface) for each value you are sorting on.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.