-
December 13th, 2005, 04:23 PM
#1
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
-
December 13th, 2005, 05:01 PM
#2
Re: sorting string arrays alphabetically
You can use the TreeSet to store your strings (in a sorted manner).
- petter
-
December 13th, 2005, 07:29 PM
#3
Re: sorting string arrays alphabetically
-
December 14th, 2005, 01:36 PM
#4
Re: sorting string arrays alphabetically
Originally Posted by riscoh
so the compair makes your strings into a int so they can be sorted or something like that?
-
December 14th, 2005, 09:54 PM
#5
Re: sorting string arrays alphabetically
java.util.Collections.sort
"The Chicken and Rice MRE is not a personal lubricant."
-
December 15th, 2005, 01:29 PM
#6
Re: sorting string arrays alphabetically
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
-
December 16th, 2005, 06:29 AM
#7
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
-
December 16th, 2005, 10:28 AM
#8
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."
-
December 16th, 2005, 11:56 AM
#9
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
-
December 16th, 2005, 11:12 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|