CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2002
    Posts
    73

    sorting string arrays alphabetically

    i allready know string arrays can be sorted alphabetically using the bubble sort and other sorting methods. i was just wondering if there is a class out-there somewhere that allows this, or something that is build into java that lets me do this with ease without using the old school sorting ways...

    thanks

    lance

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: sorting string arrays alphabetically

    You can use the TreeSet to store your strings (in a sorted manner).

    - petter

  3. #3
    Join Date
    Aug 2003
    Posts
    102

  4. #4
    Join Date
    Oct 2002
    Posts
    73

    Re: sorting string arrays alphabetically


    so the compair makes your strings into a int so they can be sorted or something like that?

  5. #5
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: sorting string arrays alphabetically

    java.util.Collections.sort
    "The Chicken and Rice MRE is not a personal lubricant."

  6. #6
    Join Date
    Oct 2002
    Posts
    73

    Re: sorting string arrays alphabetically

    Quote Originally Posted by Joe Nellis
    java.util.Collections.sort

    ok i got it .. here is a example..


    Code:
    import java.io.*;
    import java.util.*;
    
    public  class bubbleSort
    {
    	public static void main(String [] args)throws IOException
    	{
    		String [] stringArray = new String[20];
    		int count = 8;
    
    		stringArray[0] = "z";
    		stringArray[1] = "d";
    		stringArray[2] = "b";
    		stringArray[3] = "a";
    		stringArray[4] = "g";
    		stringArray[5] = "m";
    		stringArray[6] = "p";
    		stringArray[7] = "i";
    	
    		sortArray(count, stringArray);
    
    		System.out.println("---final---");
    		for(int i = 0; i < count; i++)
    			System.out.print(stringArray[i] + "  ");
    
    		System.out.println();
    
    	}
    
    
    	private static void sortArray(int numberofItems,
    								  String list[])
    	{
    		boolean exchangeMade;
    		int pass = 0;
    		String temp;
    
    		exchangeMade = true;
    
    		while((pass < numberofItems - 1) && exchangeMade == true)
    		{
    			exchangeMade = false;
    			pass++;
    			for(int index = 0; index < (numberofItems - pass); index++)
    			{
    				System.out.println();
    				if(list[index].compareTo(list[index + 1]) > 0)
    				{
    					temp = list[index];
    					list[index] = list[index + 1];
    					list[index + 1] = temp;
    					exchangeMade = true;
    				}
    				for(int i = 0; i < 8; i++)
    					System.out.print(list[i] + " ");
    				
    			}
    		}
    
    	}
    }

    there is nothing simpler like

    stringArray.sortAlpha()

    or something like that?... unless i put this in a class

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: sorting string arrays alphabetically

    there is nothing simpler like

    stringArray.sortAlpha()

    or something like that?...
    Looking at your code you don't use java.util.Collections.sort(...) at all.

    - petter

  8. #8
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: sorting string arrays alphabetically

    If you read String documentation you would see it overides the Comparable interface. This implies that the String object has a natural ordering. The ordering is lexographic (alphabetical more or less). Only objects that have a 'natural ordering' can be sorted OR you can provide a an anonymouse inner Comparable class to sort however you wish, overriding the objects natural ordering. Someone or I can give an example later.
    "The Chicken and Rice MRE is not a personal lubricant."

  9. #9
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: sorting string arrays alphabetically

    Here is an example of sorting string (in an ArrayList>

    Code:
    java.util.ArrayList<String> pList = new java.util.ArrayList<String>();
            
    // add elements to list
    pList.add("d");
    pList.add("a");
    pList.add("c");
    pList.add("b");
    
    // iterate through list
    for (String str : pList)
        System.out.println(str);
            
    // sort list
    java.util.Collections.sort(pList);
            
    // iterate through list
    for (String str : pList)
        System.out.println(str);
    - petter

  10. #10
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    Re: sorting string arrays alphabetically

    The sort method also takes a comparator which you can specify to pull an ordering out of the list. This creates and inline implementation of the Comparator interface

    Code:
    // sort list
    java.util.Collections.sort(pList, new Comparator<String>(){
    
      //override the compare method
      public int compare(String dis, String dat){
          // order by string length
          return dis.length() - dat.length();
      }
    
    });  // note brace, paranthesis closure. This ends the sort method call.
    "The Chicken and Rice MRE is not a personal lubricant."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured