Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2006, 9:59 PM   #1
oNe8
Newbie
 
oNe8's Avatar
 
Join Date: Jan 2006
Posts: 17
Rep Power: 0 oNe8 is on a distinguished road
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.
oNe8 is offline   Reply With Quote
Old Feb 22nd, 2006, 10:40 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
You could just implement one of the more efficient sort algorithms, like merge sort or quicksort. It doesn't require much effort.
jaeusm is offline   Reply With Quote
Old Feb 22nd, 2006, 10:59 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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()).
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC