• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java bubble sort

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I’m trying to bubble sort an array .

Example:

String str[]= {“ New Mexico, Santa Fe“, “Idaho, Boise”, “Nebraska, Lincoln”};

Output should be :

Boise Idaho
Lincoln Nebraska
Santa Fe New Mexico

I can bubble sort it Alphabetically by state. Can someone point me in the right direction to reverse the sort to do the second part of each string. Thanks

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

reverse the sort to do the second part of each string


What is the second part of the String?  Do you now want to sort the array by city name?

Your sample array is not good for showing a different sort since the order is the same if by state or by city.
 
Scotty Arveson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I am trying to sort by city. I am able to sort the array alphabetically by state.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take your existing code and, where you compare the result of getState(oneString) to getState(anotherString), replace those method calls by calls to getCity().

But maybe you didn't write a getState(String) method and you just have the code to get the "state" part of the string tossed in amongst the sorting logic. If not, then it would be a good idea to extract that code into a getState() method.

Or worse still, maybe you just sorted on the whole string. This would still be a good time to write getState() and getCity() methods.
 
reply
    Bookmark Topic Watch Topic
  • New Topic