This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes Vector sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector sorting" Watch "Vector sorting" New topic

Vector sorting

madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148

i have a vector that contains the following
a class oblect that contains city name and code.

i need to sort this vector so that the city names are in ascending order

this is how it looks

where N1 = city name
C1 = city code

after sorting


whete N1,C1 part of an object

is there any simple way of sorting

Money for nothing and Java for Free
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
Have the class implement Comparable and make the compareTo method sort on the city name. Then, all you have to do is use Collections.sort(). Or, make a Comparator and essentially do the same thing.
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
This is a very good link to understand Comparator and Comparable

I agree. Here's the link:
subject: Vector sorting
It's not a secret anymore!