Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Sorting by SURNAME

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to read in a list of phone book entries and sort them alphabetically by SURNAME...no joy so far! Could any one help me? Is there a way to do it without using throw and catch? Thanks.
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a working code example for you:
SortBy.java
Thanks!
Roseanne
Join our SCJD Study Group when certified


[This message has been edited by Roseanne Zhang (edited March 11, 2001).]
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also create a class that implements Comparable and use it as a holder for your phone book record. Make an array of them (one per record) ... sort on the desired field using Arrays.sort(Object[]). You will need the holder to have a method of specifying which field is to be compared and all primatives will have to be converted to their object representations.
I may not be explaining it very clearly, but I have used this method numerous times and it works quite well. I'll try to dig up some sample code and re-post.
Hope this helps

------------------
Chris Stehno (Sun Certified Programmer for the Java 2 Platform)
 
Tony Cossie
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
I'm still struggling a bit but i'm alot further on now! Thanks for the help!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help how do u sort by surname ,cant open sortby java, need help urgently, matter of life and death
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


matter of life and death


No its not.

Given that example was posted in 2001, I'm not surprised its gone.

You can start by looking at the java.util.Arrays class, and reading the JavaDocs for its various sort methods.
 
billie banjo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi paul
ive read the methods associated with sorting arrays in the java classes, however i am not able implement them ,also isnt there a easier way by just using a simple selection sort algorithm
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Billie, first is Billie Banjo your real first and last name?

Two, look at the Comparable and Comparator interface, you can define your own sorts this way, and then you can pass your Collection, and the Comparator to the Collections.sort(Collection, Comparator) method, or just the Collection to the Collections.sort(COllection) method if your classes inside the Collection implement the Comparable interface.

Mark
 
billie banjo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks mark sorry for soundind desperate but iam any way will give it a go and yes iam The BILLE BANJAO!
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic