Hi..I just want to ask for a help.
I have an Array of Object Person which has these attributes
1.first name(of type String)
2.middle initial(of type character)
3.last name (of type String)
4.age(of type int)
Now my problem is, how to sort this in ascending order?
My basis of sorting is the "age variable".

Say for example:
/*inputs*/
{
*Person1
first Name:mike
middle initial:m
last name:martin
age:19

*Person2
Name:frank
middle initial:s
last name:gaze
age:17

**Person3
Name:cindy
middle initial:f
last name:hallow
age:12}//end inputs
So my goal is to sort that Array of Object Person(using selection sort, and the basis of sorting is the "age variable".

So that the output, would something look like this.
/*outputs*/
{
**Person3
Name:cindy
middle initial:f
last name:hallow
age:12//since her age is the smallest

*Person2
Name:frank
middle initial:s
last name:gaze
age:17//since his age is bigger than of cindy

*Person1
first Name:mike
middle initial:m
last name:martin
age:19//since his age is bigger than of frank,and cindy
}//end outputs
*that's it... pls help...I don,t know to sort it...I really need to finish my program...help me guys...

thanks... in advance..

_lightner

Recommended Answers

All 2 Replies

From the Sun website:

sort

public static <T> void sort(T[] a, Comparator<? super T> 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

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)

That should be the one you're looking for.
Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.