You might want to check out this link:
http://www.samspublishing.com/articles/article.asp?p=31526&seqNum=5&rl=1
It gives an example of object sorting.
Normally, the way we do it in C++ would be applied to JAVA objects in an array or vector. You could create another empty array, and then in a while or for loop, you can compare the first object with the second object and then use a "key" to generate their order using sorting numbers. Once you do the compare, just create a new array with the new sorted objects and then you will have yourself an array with sorted objects.
2006-08-13 06:57:46
·
answer #1
·
answered by Sean I.T ? 7
·
0⤊
0⤋
From the javadoc:
sort
public static void sort(Object[] a,
Comparator c)
Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.
Parameters:
a - the array to be sorted.
c - the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
See Also:
Comparator
So, basically, you need to create a Comparator class and then create an object instance of that class. That class then needs to have a method called compare that is a binary predicate. That is, it takes two arguments and returns an integer indicating how the first parameter is related to the second parameter according to the comparison you require (in this case, an alphabetical one).
Ex: say you have made your comparator class and an object of that class called "c".
say we call c.compare ("bob", "bark");
since "bob" comes alphabetically after "bark", you return "1".
Hint: take advantage of the String method in java called "compareTo"
so, e1.compareTo (e2)
I won't the whole class for you because if this is a homework assignment you need to do it yourself.
Hopes this helps.
2006-08-13 07:01:17
·
answer #2
·
answered by celesoft 1
·
0⤊
0⤋
howdy guy, it incredibly is Generics with a BitType Array, your algorhythm is: null ? authentic : fake. the situation with Generics is the series of a catalogue is corrupted over the years till you Sychronize, and that provides complexity overhead it is incredibly uncalled for. I surely have accomplished such as what you have utilising no longer something yet comma, deliminated String. have self belief me, it is the least confusing way. in case you already know StringBuilder, it is the quickest way. The JRE makes use of StringBuilder internally FYI. At 15000 interations, StringBuilder beats Array, Generics and String s + s1 by utilising a great element. basically BitArray is speedier. I surely have additionally long previous deep into heirarchy precis classification merchandise would have boolean isInTheBag, meaning RolePlayer has application of the article. For the needs of this communicate, I back emphasize String, writing convenience strategies utilising StringBuilder.
2016-11-04 12:12:36
·
answer #3
·
answered by Anonymous
·
0⤊
0⤋
You can do it in 2 ways:--
1.
// Needs to import java.util.*;
Object[] data = {"Kiwi","Banana","Mango","Aubergine","Strawberry"};
List list = Arrays.asList(data);
Collections.sort(list);
System.out.println(list);
// Displays [Aubergine, Banana, Kiwi, Mango, Strawberry]
2.
public class SortArrayString
{
public static void sortEm(String [] array, int len)
{
int a,b;
String temp;
int sortTheStrings = len - 1;
for (a = 0; a < sortTheStrings; ++a)
for (b = 0; b < sortTheStrings; ++b)
if(array[b].compareTo(array[b + 1]) >0)
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
"Aubergine".compareTo("Banana") < 0
"Banana" .compareTo("Aubergine") > 0
"Aubergine".compareTo("Aubergine") == 0
Have Fun!!!!!!
2006-08-13 07:54:26
·
answer #4
·
answered by rohitn23 s 1
·
0⤊
0⤋
I don't know java .. but I think the idea is to arrange the characters due to their ASCII code ... the smaller character goes up ... and the bigger goes down ...
2006-08-13 06:53:52
·
answer #5
·
answered by Luay14 6
·
0⤊
1⤋