![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 17
Rep Power: 0
![]() |
Sorting an array of objects
Ok this one has me pretty stumped for something that should seem very simple.
SO i have an object[] that i need to sort by in alabetical order by the String returned by object.getName(). I suspose this can be done some how by using Arrays.sort(object[], Comparator c) but i have no idea how i would go about that. I the only reason i really want the array sorted it just so i can save processing power by using a binary search later on rather then brute forcing the hell out of the array.
__________________
http://img523.imageshack.us/img523/8436/user9kz.jpg |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
You could just implement one of the more efficient sort algorithms, like merge sort or quicksort. It doesn't require much effort.
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
Create a class, say ObjectNameComparator, that extends Comparator and implements public int compare(Object obj1, Object obj2) and returns -1 if obj1 is larger than obj2, 0 if they are the same, and 1 if obj1 is smaller.
In your main method, import java.util.Arrays. Create a new instance of ObjectNameComparator. Sort the array object[] using Arrays.sort(object, new ObjectNameComparator()). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|