Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007, 06:29 AM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
how to sort
what is the way to sort alphabetically strings from an array?

(for example lets say we have the strings "Apple", "Key" and "Zebra" in an array of 5 elements.)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 08:56 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Sorting Strings
String implements the Comparable interface so we can use the compareTo method for sorting. See first paragraph and "Classes Implementing Comparable" table at Object Ordering for bird's–eye view.
Code:
public class Sorting { public static void main(String[] args) { String[] array = { "five", "Apple", "Zebra", "Key", "elements" }; sort(array); print(array); sortIgnoreCase(array); print(array); } private static void sort(String[] strs) { for(int j = 0; j < strs.length; j++) { String minValue = strs[j]; int minIndex = j; for(int k = j+1; k < strs.length; k++) { if(strs[k].compareTo(minValue) < 1) { minValue = strs[k]; minIndex = k; } } if(minIndex != j) { // found a lower value - swap elements String temp = strs[j]; strs[j] = minValue; strs[minIndex] = temp; } } } private static void sortIgnoreCase(String[] strs) { for(int j = 0; j < strs.length; j++) { String minValue = strs[j].toLowerCase(); int minIndex = j; for(int k = j+1; k < strs.length; k++) { if(strs[k].toLowerCase().compareTo(minValue) < 1) { minValue = strs[k].toLowerCase(); minIndex = k; } } if(minIndex != j) { // found a lower value - swap elements String temp = strs[j]; strs[j] = minValue; strs[minIndex] = temp; } } } private static void print(String[] strs) { for(int j = 0; j < strs.length; j++) { System.out.print(strs[j]); if(j < strs.length-1) System.out.print(", "); } System.out.println(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
need help with bubble sort lowpro New To Java 3 12-17-2007 07:27 PM
sort Camden New To Java 7 11-28-2007 03:11 AM
Heap Sort kesav2005 Advanced Java 1 11-13-2007 01:40 PM
how to sort 2 tables valery AWT / Swing 1 08-06-2007 10:30 PM


All times are GMT +3. The time now is 03:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org