LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2003, 12:48 PM   #1
Hewin
LQ Newbie
 
Registered: Apr 2003
Posts: 10

Rep: Reputation: 0
Java help


I've looked through the API but not found anything I'm trying to find out if there is a way to sort a vector of objects on a field in that object.
Are there any methods to do this or do I have to write one?
 
Old 04-09-2003, 03:31 PM   #2
Hewin
LQ Newbie
 
Registered: Apr 2003
Posts: 10

Original Poster
Rep: Reputation: 0
I found a solution... if anyone else is interested....

make the class that the vector holds implement Comparable.

then create the following method in that class

public int compareTo(Object o){
CLASSNAME that = (CLASSNAME)o;
return this.FIELDNAME.compareTo(that.FIELDNAME;
}

Then you can call:

Collections.sort(vector);

(Dont forget to to import java.util.Collections)

this should sort it!
 
Old 04-09-2003, 03:32 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
From my understanding in Java
vector implements List, which can be
sorted. All you'd have to do is make
the objects in your vector to be
comparable by the field you want
to sort them by?

Cheers,
Tink
 
Old 04-09-2003, 03:33 PM   #4
Hewin
LQ Newbie
 
Registered: Apr 2003
Posts: 10

Original Poster
Rep: Reputation: 0
.

lol

thanks anyway tink!
 
Old 04-09-2003, 04:46 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Heh ... we had finished our posts at about the same time ...
sorry mate :}

Cheers,
Tink
 
Old 04-10-2003, 02:56 AM   #6
acjt
Member
 
Registered: Dec 2002
Location: Australia
Distribution: Gentoo
Posts: 161

Rep: Reputation: 30
You've solved your problem, however I notice you are using a Vector, are you sure you really need a vector? These objects are (the last I knew) on their way out of the api. You should look at ArrayLists and other objects in the collections api. Just thought I'd poke my nose in!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
Web start java not working (java works fine) powadha Debian 5 06-05-2004 12:57 PM
Updated Java... now java apps dont work zidane2010 Linux - Software 6 05-04-2004 08:02 PM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration