Saturday, May 22, 2010

Sorting a array of objects according to a characteristic in each object in java?

I want to sort a set of objects in a array according to particular characteristic in each object. Like the object in array[0] name is "billy" and the object in array[1] name is "Adam" I want the object with the name = adam to be sorted so it is placed first in the array, you know like alphabetically. Now if I were doign this in C++ I could just like over-load a operator and compare those two things between objects and switch them around using a bubble sort or something. But it doesn't seem like you can over-load a operator in java, which kinda sucks, how would I do this whole "sorting of objects" if you will in java?

Sorting a array of objects according to a characteristic in each object in java?
You might want to check out this link:


http://www.samspublishing.com/articles/a...





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.
Reply: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 ...
Reply:You can do it in 2 ways:--


1.





// Needs to import java.util.*;


Object[] data = {"Kiwi","Banana","Mango","Aubergine","St...


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 %26lt; sortTheStrings; ++a)


for (b = 0; b %26lt; sortTheStrings; ++b)


if(array[b].compareTo(array[b + 1]) %26gt;0)


{


temp = array[b];


array[b] = array[b + 1];


array[b + 1] = temp;


}





}


}





"Aubergine".compareTo("Banana") %26lt; 0


"Banana" .compareTo("Aubergine") %26gt; 0


"Aubergine".compareTo("Aubergine") == 0





Have Fun!!!!!!
Reply: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.


No comments:

Post a Comment