This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Android and the fly likes sort an arraylist of custom objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "sort an arraylist of custom objects" Watch "sort an arraylist of custom objects" New topic
Author

sort an arraylist of custom objects

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

Good morning,
I have a array list that contains a number of custom objects.
My object has the below structure :
myObj.ID
myObj.Name
myObj.Date

is there a way i can sort this array list by id or name? The data comes from a web service not in an ordered way.

Thanks in advance.
Vishal Trivedi
Ranch Hand

Joined: Sep 05, 2011
Posts: 57
Hii


Why dont you try creating a comparator class.
The Class implements Comparator and its method compare to do comparison ...
you can make a condition here that can do sorting based on your preference.
Then call Collection.sort(urList,new instanceOfComparator());
you can modify the constructor to pass your preference like
Collection.sort(urList,new instanceOfComparator("myPrefernce"));



Just dont forget to use compareTO for comparison of String to have more accurate result.

Please correct me if i am wronng or have made a wrong interpretation of your question.
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

Hello i did what you said and it works perfectly. One more question, not opening new post because it is for the same list of objects, how can i filter this list of objects from text in a edittext?
Can this be done dynamically or is there another way?

thanks in advance.
Vishal Trivedi
Ranch Hand

Joined: Sep 05, 2011
Posts: 57
i am sorry i dint not understand your question. properly i guess


you can use an AutoCompleteTextView instead of EditText this will work just like conact list from your cell phone fetching data dynamically from a predefined list as you type.
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

I have a array list that contains a number of custom objects.
My object has the below structure :
myObj.ID
myObj.Name
myObj.Date

is there a way i can filter this array by its name or date in any way?
Vishal Trivedi
Ranch Hand

Joined: Sep 05, 2011
Posts: 57
can you not introduce a radoButton group containing the list of elements(name and Date) that may be used for sorting.And based on user's selection the key for sorting could be set.
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

i am actually want help on how to filter the objects in the arraylist, my sorting function works.

thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sort an arraylist of custom objects
 
Similar Threads
About Comparable and Comparator
Sorting question
Sorting XML nodes?
Can I sort String arrays?
Randomly shuffling an array.