File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector sorting

 
madhup narain
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi


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
------------------------
N4,C4|N1,C1|N3,C3|N2,C2|
------------------------

where N1 = city name
C1 = city code

after sorting

------------------------
N1,C1|N2,C2|N3,C3|N4,C4|
------------------------

whete N1,C1 part of an object

is there any simple way of sorting
 
Craig Tyler
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very good link to understand Comparator and Comparable http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-sort.html
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic